{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:49:40.916821800Z",
     "start_time": "2024-05-26T11:49:39.487727900Z"
    }
   },
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "from sklearn.datasets import load_iris, fetch_20newsgroups, fetch_california_housing\n",
    "from sklearn.model_selection import train_test_split, GridSearchCV\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "from sklearn.naive_bayes import MultinomialNB\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.feature_extraction import DictVectorizer\n",
    "from sklearn.tree import DecisionTreeClassifier, export_graphviz\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.metrics import roc_auc_score"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 1 加载数据集\n",
    "### load直接加载到内存，数据集比较小，并不会保存到本地磁盘\n",
    "### fetch数据集比较大，下载下来后会存在本地磁盘，下一次就不会再连接sklearn的服务器\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'sklearn.utils._bunch.Bunch'>\n",
      "--------------------------------------------------\n",
      "<class 'numpy.ndarray'>\n",
      "(150, 4)\n"
     ]
    },
    {
     "data": {
      "text/plain": "array([[5.1, 3.5, 1.4, 0.2],\n       [4.9, 3. , 1.4, 0.2],\n       [4.7, 3.2, 1.3, 0.2],\n       [4.6, 3.1, 1.5, 0.2],\n       [5. , 3.6, 1.4, 0.2],\n       [5.4, 3.9, 1.7, 0.4],\n       [4.6, 3.4, 1.4, 0.3],\n       [5. , 3.4, 1.5, 0.2],\n       [4.4, 2.9, 1.4, 0.2],\n       [4.9, 3.1, 1.5, 0.1],\n       [5.4, 3.7, 1.5, 0.2],\n       [4.8, 3.4, 1.6, 0.2],\n       [4.8, 3. , 1.4, 0.1],\n       [4.3, 3. , 1.1, 0.1],\n       [5.8, 4. , 1.2, 0.2],\n       [5.7, 4.4, 1.5, 0.4],\n       [5.4, 3.9, 1.3, 0.4],\n       [5.1, 3.5, 1.4, 0.3],\n       [5.7, 3.8, 1.7, 0.3],\n       [5.1, 3.8, 1.5, 0.3],\n       [5.4, 3.4, 1.7, 0.2],\n       [5.1, 3.7, 1.5, 0.4],\n       [4.6, 3.6, 1. , 0.2],\n       [5.1, 3.3, 1.7, 0.5],\n       [4.8, 3.4, 1.9, 0.2],\n       [5. , 3. , 1.6, 0.2],\n       [5. , 3.4, 1.6, 0.4],\n       [5.2, 3.5, 1.5, 0.2],\n       [5.2, 3.4, 1.4, 0.2],\n       [4.7, 3.2, 1.6, 0.2],\n       [4.8, 3.1, 1.6, 0.2],\n       [5.4, 3.4, 1.5, 0.4],\n       [5.2, 4.1, 1.5, 0.1],\n       [5.5, 4.2, 1.4, 0.2],\n       [4.9, 3.1, 1.5, 0.2],\n       [5. , 3.2, 1.2, 0.2],\n       [5.5, 3.5, 1.3, 0.2],\n       [4.9, 3.6, 1.4, 0.1],\n       [4.4, 3. , 1.3, 0.2],\n       [5.1, 3.4, 1.5, 0.2],\n       [5. , 3.5, 1.3, 0.3],\n       [4.5, 2.3, 1.3, 0.3],\n       [4.4, 3.2, 1.3, 0.2],\n       [5. , 3.5, 1.6, 0.6],\n       [5.1, 3.8, 1.9, 0.4],\n       [4.8, 3. , 1.4, 0.3],\n       [5.1, 3.8, 1.6, 0.2],\n       [4.6, 3.2, 1.4, 0.2],\n       [5.3, 3.7, 1.5, 0.2],\n       [5. , 3.3, 1.4, 0.2],\n       [7. , 3.2, 4.7, 1.4],\n       [6.4, 3.2, 4.5, 1.5],\n       [6.9, 3.1, 4.9, 1.5],\n       [5.5, 2.3, 4. , 1.3],\n       [6.5, 2.8, 4.6, 1.5],\n       [5.7, 2.8, 4.5, 1.3],\n       [6.3, 3.3, 4.7, 1.6],\n       [4.9, 2.4, 3.3, 1. ],\n       [6.6, 2.9, 4.6, 1.3],\n       [5.2, 2.7, 3.9, 1.4],\n       [5. , 2. , 3.5, 1. ],\n       [5.9, 3. , 4.2, 1.5],\n       [6. , 2.2, 4. , 1. ],\n       [6.1, 2.9, 4.7, 1.4],\n       [5.6, 2.9, 3.6, 1.3],\n       [6.7, 3.1, 4.4, 1.4],\n       [5.6, 3. , 4.5, 1.5],\n       [5.8, 2.7, 4.1, 1. ],\n       [6.2, 2.2, 4.5, 1.5],\n       [5.6, 2.5, 3.9, 1.1],\n       [5.9, 3.2, 4.8, 1.8],\n       [6.1, 2.8, 4. , 1.3],\n       [6.3, 2.5, 4.9, 1.5],\n       [6.1, 2.8, 4.7, 1.2],\n       [6.4, 2.9, 4.3, 1.3],\n       [6.6, 3. , 4.4, 1.4],\n       [6.8, 2.8, 4.8, 1.4],\n       [6.7, 3. , 5. , 1.7],\n       [6. , 2.9, 4.5, 1.5],\n       [5.7, 2.6, 3.5, 1. ],\n       [5.5, 2.4, 3.8, 1.1],\n       [5.5, 2.4, 3.7, 1. ],\n       [5.8, 2.7, 3.9, 1.2],\n       [6. , 2.7, 5.1, 1.6],\n       [5.4, 3. , 4.5, 1.5],\n       [6. , 3.4, 4.5, 1.6],\n       [6.7, 3.1, 4.7, 1.5],\n       [6.3, 2.3, 4.4, 1.3],\n       [5.6, 3. , 4.1, 1.3],\n       [5.5, 2.5, 4. , 1.3],\n       [5.5, 2.6, 4.4, 1.2],\n       [6.1, 3. , 4.6, 1.4],\n       [5.8, 2.6, 4. , 1.2],\n       [5. , 2.3, 3.3, 1. ],\n       [5.6, 2.7, 4.2, 1.3],\n       [5.7, 3. , 4.2, 1.2],\n       [5.7, 2.9, 4.2, 1.3],\n       [6.2, 2.9, 4.3, 1.3],\n       [5.1, 2.5, 3. , 1.1],\n       [5.7, 2.8, 4.1, 1.3],\n       [6.3, 3.3, 6. , 2.5],\n       [5.8, 2.7, 5.1, 1.9],\n       [7.1, 3. , 5.9, 2.1],\n       [6.3, 2.9, 5.6, 1.8],\n       [6.5, 3. , 5.8, 2.2],\n       [7.6, 3. , 6.6, 2.1],\n       [4.9, 2.5, 4.5, 1.7],\n       [7.3, 2.9, 6.3, 1.8],\n       [6.7, 2.5, 5.8, 1.8],\n       [7.2, 3.6, 6.1, 2.5],\n       [6.5, 3.2, 5.1, 2. ],\n       [6.4, 2.7, 5.3, 1.9],\n       [6.8, 3. , 5.5, 2.1],\n       [5.7, 2.5, 5. , 2. ],\n       [5.8, 2.8, 5.1, 2.4],\n       [6.4, 3.2, 5.3, 2.3],\n       [6.5, 3. , 5.5, 1.8],\n       [7.7, 3.8, 6.7, 2.2],\n       [7.7, 2.6, 6.9, 2.3],\n       [6. , 2.2, 5. , 1.5],\n       [6.9, 3.2, 5.7, 2.3],\n       [5.6, 2.8, 4.9, 2. ],\n       [7.7, 2.8, 6.7, 2. ],\n       [6.3, 2.7, 4.9, 1.8],\n       [6.7, 3.3, 5.7, 2.1],\n       [7.2, 3.2, 6. , 1.8],\n       [6.2, 2.8, 4.8, 1.8],\n       [6.1, 3. , 4.9, 1.8],\n       [6.4, 2.8, 5.6, 2.1],\n       [7.2, 3. , 5.8, 1.6],\n       [7.4, 2.8, 6.1, 1.9],\n       [7.9, 3.8, 6.4, 2. ],\n       [6.4, 2.8, 5.6, 2.2],\n       [6.3, 2.8, 5.1, 1.5],\n       [6.1, 2.6, 5.6, 1.4],\n       [7.7, 3. , 6.1, 2.3],\n       [6.3, 3.4, 5.6, 2.4],\n       [6.4, 3.1, 5.5, 1.8],\n       [6. , 3. , 4.8, 1.8],\n       [6.9, 3.1, 5.4, 2.1],\n       [6.7, 3.1, 5.6, 2.4],\n       [6.9, 3.1, 5.1, 2.3],\n       [5.8, 2.7, 5.1, 1.9],\n       [6.8, 3.2, 5.9, 2.3],\n       [6.7, 3.3, 5.7, 2.5],\n       [6.7, 3. , 5.2, 2.3],\n       [6.3, 2.5, 5. , 1.9],\n       [6.5, 3. , 5.2, 2. ],\n       [6.2, 3.4, 5.4, 2.3],\n       [5.9, 3. , 5.1, 1.8]])"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#鸢尾花数据集，查看特征，目标，样本量\n",
    "\n",
    "li = load_iris()\n",
    "\n",
    "print(type(li)) # <class 'sklearn.utils._bunch.Bunch'>，并不是df，所以打印看不到拼在一起的 特征列+目标列\n",
    "# print(li)\n",
    "print('-' * 50)\n",
    "\n",
    "# 喂给模型的都是ndarray\n",
    "print(type(li.data))\n",
    "print(li.data.shape) # 150个样本，4个特征\n",
    "\n",
    "li.data"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:49:41.040754600Z",
     "start_time": "2024-05-26T11:49:40.920832700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
      " 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n",
      " 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2\n",
      " 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\n",
      " 2 2]\n",
      "<class 'numpy.ndarray'>\n",
      "--------------------------------------------------\n",
      "['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']\n",
      "<class 'list'>\n",
      "--------------------------------------------------\n",
      "['setosa' 'versicolor' 'virginica']\n"
     ]
    }
   ],
   "source": [
    "print(li.target)  # 目标值，即标签，可以看到鸢尾花被分为3个类别\n",
    "print(type(li.target))\n",
    "print('-' * 50)\n",
    "\n",
    "# print(li.DESCR) # 数据集描述\n",
    "# print('-' * 50)\n",
    "\n",
    "print(li.feature_names)  # 重点,特征名字\n",
    "print(type(li.feature_names))\n",
    "print('-' * 50)\n",
    "\n",
    "print(li.target_names) # 目标名字，回归就没有这个"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:49:41.045740500Z",
     "start_time": "2024-05-26T11:49:40.953869600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[6.5 2.8 4.6 1.5]\n",
      " [6.7 2.5 5.8 1.8]\n",
      " [6.8 3.  5.5 2.1]\n",
      " [5.1 3.5 1.4 0.3]\n",
      " [6.  2.2 5.  1.5]\n",
      " [6.3 2.9 5.6 1.8]\n",
      " [6.6 2.9 4.6 1.3]\n",
      " [7.7 2.6 6.9 2.3]\n",
      " [5.7 3.8 1.7 0.3]\n",
      " [5.  3.6 1.4 0.2]\n",
      " [4.8 3.  1.4 0.3]\n",
      " [5.2 2.7 3.9 1.4]\n",
      " [5.1 3.4 1.5 0.2]\n",
      " [5.5 3.5 1.3 0.2]\n",
      " [7.7 3.8 6.7 2.2]\n",
      " [6.9 3.1 5.4 2.1]\n",
      " [7.3 2.9 6.3 1.8]\n",
      " [6.4 2.8 5.6 2.2]\n",
      " [6.2 2.8 4.8 1.8]\n",
      " [6.  3.4 4.5 1.6]\n",
      " [7.7 2.8 6.7 2. ]\n",
      " [5.7 3.  4.2 1.2]\n",
      " [4.8 3.4 1.6 0.2]\n",
      " [5.7 2.5 5.  2. ]\n",
      " [6.3 2.7 4.9 1.8]\n",
      " [4.8 3.  1.4 0.1]\n",
      " [4.7 3.2 1.3 0.2]\n",
      " [6.5 3.  5.8 2.2]\n",
      " [4.6 3.4 1.4 0.3]\n",
      " [6.1 3.  4.9 1.8]\n",
      " [6.5 3.2 5.1 2. ]\n",
      " [6.7 3.1 4.4 1.4]\n",
      " [5.7 2.8 4.5 1.3]\n",
      " [6.7 3.3 5.7 2.5]\n",
      " [6.  3.  4.8 1.8]\n",
      " [5.1 3.8 1.6 0.2]\n",
      " [6.  2.2 4.  1. ]\n",
      " [6.4 2.9 4.3 1.3]\n",
      " [6.5 3.  5.5 1.8]\n",
      " [5.  2.3 3.3 1. ]\n",
      " [6.3 3.3 6.  2.5]\n",
      " [5.5 2.5 4.  1.3]\n",
      " [5.4 3.7 1.5 0.2]\n",
      " [4.9 3.1 1.5 0.2]\n",
      " [5.2 4.1 1.5 0.1]\n",
      " [6.7 3.3 5.7 2.1]\n",
      " [4.4 3.  1.3 0.2]\n",
      " [6.  2.7 5.1 1.6]\n",
      " [6.4 2.7 5.3 1.9]\n",
      " [5.9 3.  5.1 1.8]\n",
      " [5.2 3.5 1.5 0.2]\n",
      " [5.1 3.3 1.7 0.5]\n",
      " [5.8 2.7 4.1 1. ]\n",
      " [4.9 3.1 1.5 0.1]\n",
      " [7.4 2.8 6.1 1.9]\n",
      " [6.2 2.9 4.3 1.3]\n",
      " [7.6 3.  6.6 2.1]\n",
      " [6.7 3.  5.2 2.3]\n",
      " [6.3 2.3 4.4 1.3]\n",
      " [6.2 3.4 5.4 2.3]\n",
      " [7.2 3.6 6.1 2.5]\n",
      " [5.6 2.9 3.6 1.3]\n",
      " [5.7 4.4 1.5 0.4]\n",
      " [5.8 2.7 3.9 1.2]\n",
      " [4.5 2.3 1.3 0.3]\n",
      " [5.5 2.4 3.8 1.1]\n",
      " [6.9 3.1 4.9 1.5]\n",
      " [5.  3.4 1.6 0.4]\n",
      " [6.8 2.8 4.8 1.4]\n",
      " [5.  3.5 1.6 0.6]\n",
      " [4.8 3.4 1.9 0.2]\n",
      " [6.3 3.4 5.6 2.4]\n",
      " [5.6 2.8 4.9 2. ]\n",
      " [6.8 3.2 5.9 2.3]\n",
      " [5.  3.3 1.4 0.2]\n",
      " [5.1 3.7 1.5 0.4]\n",
      " [5.9 3.2 4.8 1.8]\n",
      " [4.6 3.1 1.5 0.2]\n",
      " [5.8 2.7 5.1 1.9]\n",
      " [4.8 3.1 1.6 0.2]\n",
      " [6.5 3.  5.2 2. ]\n",
      " [4.9 2.5 4.5 1.7]\n",
      " [4.6 3.2 1.4 0.2]\n",
      " [6.4 3.2 5.3 2.3]\n",
      " [4.3 3.  1.1 0.1]\n",
      " [5.6 3.  4.1 1.3]\n",
      " [4.4 2.9 1.4 0.2]\n",
      " [5.5 2.4 3.7 1. ]\n",
      " [5.  2.  3.5 1. ]\n",
      " [5.1 3.5 1.4 0.2]\n",
      " [4.9 3.  1.4 0.2]\n",
      " [4.9 2.4 3.3 1. ]\n",
      " [4.6 3.6 1.  0.2]\n",
      " [5.9 3.  4.2 1.5]\n",
      " [6.1 2.9 4.7 1.4]\n",
      " [5.  3.4 1.5 0.2]\n",
      " [6.7 3.1 4.7 1.5]\n",
      " [5.7 2.9 4.2 1.3]\n",
      " [6.2 2.2 4.5 1.5]\n",
      " [7.  3.2 4.7 1.4]\n",
      " [5.8 2.7 5.1 1.9]\n",
      " [5.4 3.4 1.7 0.2]\n",
      " [5.  3.  1.6 0.2]\n",
      " [6.1 2.6 5.6 1.4]\n",
      " [6.1 2.8 4.  1.3]\n",
      " [7.2 3.  5.8 1.6]\n",
      " [5.7 2.6 3.5 1. ]\n",
      " [6.3 2.8 5.1 1.5]\n",
      " [6.4 3.1 5.5 1.8]\n",
      " [6.3 2.5 4.9 1.5]\n",
      " [6.7 3.1 5.6 2.4]\n",
      " [4.9 3.6 1.4 0.1]]\n",
      "--------------------------------------------------\n",
      "[[5.8 4.  1.2 0.2]\n",
      " [5.1 2.5 3.  1.1]\n",
      " [6.6 3.  4.4 1.4]\n",
      " [5.4 3.9 1.3 0.4]\n",
      " [7.9 3.8 6.4 2. ]\n",
      " [6.3 3.3 4.7 1.6]\n",
      " [6.9 3.1 5.1 2.3]\n",
      " [5.1 3.8 1.9 0.4]\n",
      " [4.7 3.2 1.6 0.2]\n",
      " [6.9 3.2 5.7 2.3]\n",
      " [5.6 2.7 4.2 1.3]\n",
      " [5.4 3.9 1.7 0.4]\n",
      " [7.1 3.  5.9 2.1]\n",
      " [6.4 3.2 4.5 1.5]\n",
      " [6.  2.9 4.5 1.5]\n",
      " [4.4 3.2 1.3 0.2]\n",
      " [5.8 2.6 4.  1.2]\n",
      " [5.6 3.  4.5 1.5]\n",
      " [5.4 3.4 1.5 0.4]\n",
      " [5.  3.2 1.2 0.2]\n",
      " [5.5 2.6 4.4 1.2]\n",
      " [5.4 3.  4.5 1.5]\n",
      " [6.7 3.  5.  1.7]\n",
      " [5.  3.5 1.3 0.3]\n",
      " [7.2 3.2 6.  1.8]\n",
      " [5.7 2.8 4.1 1.3]\n",
      " [5.5 4.2 1.4 0.2]\n",
      " [5.1 3.8 1.5 0.3]\n",
      " [6.1 2.8 4.7 1.2]\n",
      " [6.3 2.5 5.  1.9]\n",
      " [6.1 3.  4.6 1.4]\n",
      " [7.7 3.  6.1 2.3]\n",
      " [5.6 2.5 3.9 1.1]\n",
      " [6.4 2.8 5.6 2.1]\n",
      " [5.8 2.8 5.1 2.4]\n",
      " [5.3 3.7 1.5 0.2]\n",
      " [5.5 2.3 4.  1.3]\n",
      " [5.2 3.4 1.4 0.2]]\n",
      "[1 2 2 0 2 2 1 2 0 0 0 1 0 0 2 2 2 2 2 1 2 1 0 2 2 0 0 2 0 2 2 1 1 2 2 0 1\n",
      " 1 2 1 2 1 0 0 0 2 0 1 2 2 0 0 1 0 2 1 2 2 1 2 2 1 0 1 0 1 1 0 1 0 0 2 2 2\n",
      " 0 0 1 0 2 0 2 2 0 2 0 1 0 1 1 0 0 1 0 1 1 0 1 1 1 1 2 0 0 2 1 2 1 2 2 1 2\n",
      " 0]\n",
      "[0 1 1 0 2 1 2 0 0 2 1 0 2 1 1 0 1 1 0 0 1 1 1 0 2 1 0 0 1 2 1 2 1 2 2 0 1\n",
      " 0]\n"
     ]
    }
   ],
   "source": [
    "# 注意返回值, 训练集 train  x_train, y_train        测试集  test   x_test, y_test，顺序千万别搞错了\n",
    "# 默认是乱序的,random_state为了确保两次的随机策略一致，就会得到相同的随机数据，往往会带上\n",
    "x_train, x_test, y_train, y_test = train_test_split(li.data, li.target, test_size=0.25, random_state=1)\n",
    "\n",
    "print(x_train)\n",
    "print(\"-\"*50)\n",
    "print(x_test)\n",
    "print(y_train)\n",
    "print(y_test)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:49:41.047735400Z",
     "start_time": "2024-05-26T11:49:40.968978200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(112, 4)\n",
      "(38, 4)\n",
      "(112,)\n",
      "(38,)\n"
     ]
    }
   ],
   "source": [
    "print(x_train.shape)\n",
    "print(x_test.shape)\n",
    "print(y_train.shape)\n",
    "print(y_test.shape)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:49:41.047735400Z",
     "start_time": "2024-05-26T11:49:40.985933400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "<class 'numpy.ndarray'>\n",
      "<class 'numpy.ndarray'>\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "print(type(x_train))\n",
    "print(type(x_test))\n",
    "print(type(y_train))\n",
    "print(type(y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:49:41.049731600Z",
     "start_time": "2024-05-26T11:49:41.001890400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'sklearn.utils._bunch.Bunch'>\n",
      "--------------------------------------------------\n",
      "From: Mamatha Devineni Ratnam <mr47+@andrew.cmu.edu>\n",
      "Subject: Pens fans reactions\n",
      "Organization: Post Office, Carnegie Mellon, Pittsburgh, PA\n",
      "Lines: 12\n",
      "NNTP-Posting-Host: po4.andrew.cmu.edu\n",
      "\n",
      "\n",
      "\n",
      "I am sure some bashers of Pens fans are pretty confused about the lack\n",
      "of any kind of posts about the recent Pens massacre of the Devils. Actually,\n",
      "I am  bit puzzled too and a bit relieved. However, I am going to put an end\n",
      "to non-PIttsburghers' relief with a bit of praise for the Pens. Man, they\n",
      "are killing those Devils worse than I thought. Jagr just showed you why\n",
      "he is much better than his regular season stats. He is also a lot\n",
      "fo fun to watch in the playoffs. Bowman should let JAgr have a lot of\n",
      "fun in the next couple of games since the Pens are going to beat the pulp out of Jersey anyway. I was very disappointed not to see the Islanders lose the final\n",
      "regular season game.          PENS RULE!!!\n",
      "\n",
      "\n",
      "--------------------------------------------------\n",
      "特征类型\n",
      "<class 'list'>\n",
      "<class 'str'>\n",
      "--------------------------------------------------\n",
      "18846\n"
     ]
    }
   ],
   "source": [
    "# 下面是比较大的数据，需要下载一会，20类新闻\n",
    "# subset代表下载的数据集类型，默认是train，只有训练集\n",
    "# 先去data_home目录下查看是否有数据，没有的话就下载\n",
    "news = fetch_20newsgroups(subset='all', data_home='data')\n",
    "print(type(news))\n",
    "print(\"-\"*50)\n",
    "#print(news.DESCR)\n",
    "\n",
    "# 第一个样本\n",
    "print(news.data[0])\n",
    "print('-' * 50)\n",
    "\n",
    "print('特征类型')\n",
    "# print(news.feature_names)  #这个数据集是没有的，因为没有特征，只有文本数据\n",
    "print(type(news.data))  # list，存的是一篇篇文章\n",
    "print(type(news.data[0]))   # str，一篇文章就是一个字符串\n",
    "print('-' * 50)\n",
    "\n",
    "print(len(news.data))   # 有多少篇文章\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:49:41.307345400Z",
     "start_time": "2024-05-26T11:49:41.019844Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['alt.atheism',\n",
      " 'comp.graphics',\n",
      " 'comp.os.ms-windows.misc',\n",
      " 'comp.sys.ibm.pc.hardware',\n",
      " 'comp.sys.mac.hardware',\n",
      " 'comp.windows.x',\n",
      " 'misc.forsale',\n",
      " 'rec.autos',\n",
      " 'rec.motorcycles',\n",
      " 'rec.sport.baseball',\n",
      " 'rec.sport.hockey',\n",
      " 'sci.crypt',\n",
      " 'sci.electronics',\n",
      " 'sci.med',\n",
      " 'sci.space',\n",
      " 'soc.religion.christian',\n",
      " 'talk.politics.guns',\n",
      " 'talk.politics.mideast',\n",
      " 'talk.politics.misc',\n",
      " 'talk.religion.misc']\n",
      "--------------------------------------------------\n",
      "新闻所有的标签：\n",
      "[10  3 17 ...  3  1  7]\n",
      "<class 'numpy.ndarray'>\n",
      "[10  3 17  3  4 12  4 10 10 19 19 11 19 13  0]\n",
      "0 19\n"
     ]
    }
   ],
   "source": [
    "from pprint import pprint\n",
    "pprint(list(news.target_names)) # 打印新闻的分类名字\n",
    "print('-' * 50)\n",
    "\n",
    "print('新闻所有的标签：')\n",
    "print(news.target)\n",
    "print(type(news.target))\n",
    "print(news.target[0:15]) # 打印前15个样本的标签，即是什么类别的新闻\n",
    "print(min(news.target), max(news.target))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:49:41.322255400Z",
     "start_time": "2024-05-26T11:49:41.310337100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "#因为新版本sklearn去掉了 这个数据集，不再讲解\n",
    "# 接着来看回归的数据,是波士顿房价\n",
    "# lb = load_boston()\n",
    "#\n",
    "# print(\"获取特征值\")\n",
    "# print(lb.data[0])  #第一个样本特征值\n",
    "# print(lb.data.shape)\n",
    "# print('-' * 50)\n",
    "# print(\"目标值\")\n",
    "# print(lb.target)\n",
    "# print('-' * 50)\n",
    "# print(lb.DESCR)\n",
    "# print('-' * 50)\n",
    "# print(lb.feature_names)\n",
    "# print('-' * 50)\n",
    "# 回归问题没这个,打印这个会报错\n",
    "# print(lb.target_names)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:49:41.383099500Z",
     "start_time": "2024-05-26T11:49:41.324277700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'sklearn.utils._bunch.Bunch'>\n",
      "--------------------------------------------------\n",
      "[   8.3252       41.            6.98412698    1.02380952  322.\n",
      "    2.55555556   37.88       -122.23      ]\n",
      "<class 'numpy.ndarray'>\n",
      "(20640, 8)\n",
      "--------------------------------------------------\n",
      "目标值\n",
      "[4.526 3.585 3.521 ... 0.923 0.847 0.894]\n",
      "(20640,)\n",
      "<class 'numpy.ndarray'>\n",
      "--------------------------------------------------\n",
      "['MedInc', 'HouseAge', 'AveRooms', 'AveBedrms', 'Population', 'AveOccup', 'Latitude', 'Longitude']\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "house=fetch_california_housing(data_home='data')\n",
    "print(type(house))\n",
    "print('-' * 50)\n",
    "\n",
    "print(house.data[0])  #第一个样本\n",
    "print(type(house.data))\n",
    "print(house.data.shape)\n",
    "print('-' * 50)\n",
    "\n",
    "print(\"目标值\")\n",
    "print(house.target)\n",
    "print(house.target.shape)\n",
    "print(type(house.target))\n",
    "print('-' * 50)\n",
    "\n",
    "# print(house.DESCR)\n",
    "# print('-' * 50)\n",
    "\n",
    "print(house.feature_names)\n",
    "print('-' * 50)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:49:41.400109600Z",
     "start_time": "2024-05-26T11:49:41.343206100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 2 分类估计器\n",
    "### 估计器就是实现算法的api，\n",
    "### 比如用于分类、回归的估计器：\n",
    "### sklearn.neighbors、sklearn.tree、sklearn.naive_bayes、\n",
    "### sklearn.linear_model.LinearRegression等等"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1、 k近邻\n",
    "### 没有训练成本，直接预测，但预测成本高\n",
    "### 可以分类，也可以回归（求平均值即可）"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   row_id       x       y  accuracy    time    place_id\n",
      "0       0  0.7941  9.0809        54  470702  8523065625\n",
      "1       1  5.9567  4.7968        13  186555  1757726713\n",
      "2       2  8.3078  7.0407        74  322648  1137537235\n",
      "3       3  7.3665  2.5165        65  704587  6567393236\n",
      "4       4  4.0961  1.1307        31  472130  7440663949\n",
      "5       5  3.8099  1.9586        75  178065  6289802927\n",
      "6       6  6.3336  4.3720        13  666829  9931249544\n",
      "7       7  5.7409  6.7697        85  369002  5662813655\n",
      "8       8  4.3114  6.9410         3  166384  8471780938\n",
      "9       9  6.3414  0.0758        65  400060  1253803156\n",
      "(29118021, 6)\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 29118021 entries, 0 to 29118020\n",
      "Data columns (total 6 columns):\n",
      " #   Column    Dtype  \n",
      "---  ------    -----  \n",
      " 0   row_id    int64  \n",
      " 1   x         float64\n",
      " 2   y         float64\n",
      " 3   accuracy  int64  \n",
      " 4   time      int64  \n",
      " 5   place_id  int64  \n",
      "dtypes: float64(2), int64(4)\n",
      "memory usage: 1.3 GB\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "# K近邻\n",
    "\"\"\"\n",
    "K-近邻预测用户签到位置\n",
    ":return:None\n",
    "\"\"\"\n",
    "# 读取数据\n",
    "data = pd.read_csv(\"./data/FBlocation/train.csv\")\n",
    "\n",
    "print(data.head(10))\n",
    "print(data.shape)\n",
    "print(data.info())\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:06.772510900Z",
     "start_time": "2024-05-26T11:49:41.374122200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 处理数据"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [],
   "source": [
    "# 1、缩小数据,查询数据,为了减少计算时间\n",
    "data = data.query(\"x > 1.0 &  x < 1.25 & y > 2.5 & y < 2.75\")\n",
    "# data = data[ (data['x'] > 1.0) & (data['x'] < 1.25) & (data['y'] > 2.5) & (data['y'] < 2.75)]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:10.788901800Z",
     "start_time": "2024-05-26T11:50:06.771514200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "      row_id       x       y  accuracy    time    place_id\n600      600  1.2214  2.7023        17   65380  6683426742\n957      957  1.1832  2.6891        58  785470  6683426742\n4345    4345  1.1935  2.6550        11  400082  6889790653\n4735    4735  1.1452  2.6074        49  514983  6822359752\n5580    5580  1.0089  2.7287        19  732410  1527921905",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>row_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>time</th>\n      <th>place_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>600</th>\n      <td>600</td>\n      <td>1.2214</td>\n      <td>2.7023</td>\n      <td>17</td>\n      <td>65380</td>\n      <td>6683426742</td>\n    </tr>\n    <tr>\n      <th>957</th>\n      <td>957</td>\n      <td>1.1832</td>\n      <td>2.6891</td>\n      <td>58</td>\n      <td>785470</td>\n      <td>6683426742</td>\n    </tr>\n    <tr>\n      <th>4345</th>\n      <td>4345</td>\n      <td>1.1935</td>\n      <td>2.6550</td>\n      <td>11</td>\n      <td>400082</td>\n      <td>6889790653</td>\n    </tr>\n    <tr>\n      <th>4735</th>\n      <td>4735</td>\n      <td>1.1452</td>\n      <td>2.6074</td>\n      <td>49</td>\n      <td>514983</td>\n      <td>6822359752</td>\n    </tr>\n    <tr>\n      <th>5580</th>\n      <td>5580</td>\n      <td>1.0089</td>\n      <td>2.7287</td>\n      <td>19</td>\n      <td>732410</td>\n      <td>1527921905</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head(5)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:10.859043300Z",
     "start_time": "2024-05-26T11:50:10.787132100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "600    1970-01-01 18:09:40\n",
      "957    1970-01-10 02:11:10\n",
      "4345   1970-01-05 15:08:02\n",
      "4735   1970-01-06 23:03:03\n",
      "5580   1970-01-09 11:26:50\n",
      "6090   1970-01-02 16:25:07\n",
      "6234   1970-01-04 15:52:57\n",
      "6350   1970-01-01 10:13:36\n",
      "7468   1970-01-09 15:26:06\n",
      "8478   1970-01-08 23:52:02\n",
      "Name: time, dtype: datetime64[ns]\n"
     ]
    }
   ],
   "source": [
    "# 处理时间的数据\n",
    "time_value = pd.to_datetime(data['time'], unit='s')\n",
    "\n",
    "print(time_value.head(10))  #最大时间是1月10号"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:10.947792900Z",
     "start_time": "2024-05-26T11:50:10.846198200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [],
   "source": [
    "# 把日期格式转换成 字典格式，把年，月，日，时，分，秒转换为字典格式，变为索引\n",
    "time_value = pd.DatetimeIndex(time_value)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.066523800Z",
     "start_time": "2024-05-26T11:50:10.887393Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.indexes.datetimes.DatetimeIndex'>\n",
      "DatetimeIndex(['1970-01-01 18:09:40', '1970-01-10 02:11:10',\n",
      "               '1970-01-05 15:08:02', '1970-01-06 23:03:03',\n",
      "               '1970-01-09 11:26:50', '1970-01-02 16:25:07',\n",
      "               '1970-01-04 15:52:57', '1970-01-01 10:13:36',\n",
      "               '1970-01-09 15:26:06', '1970-01-08 23:52:02'],\n",
      "              dtype='datetime64[ns]', name='time', freq=None)\n"
     ]
    }
   ],
   "source": [
    "print(type(time_value))\n",
    "print(time_value[0:10])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.139271600Z",
     "start_time": "2024-05-26T11:50:10.904907100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index([1970, 1970, 1970, 1970, 1970, 1970, 1970, 1970, 1970, 1970,\n",
      "       ...\n",
      "       1970, 1970, 1970, 1970, 1970, 1970, 1970, 1970, 1970, 1970],\n",
      "      dtype='int32', name='time', length=17710)\n",
      "<class 'pandas.core.indexes.base.Index'>\n",
      "\n",
      "Index([1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
      "       ...\n",
      "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
      "      dtype='int32', name='time', length=17710)\n",
      "Index([ 1, 10,  5,  6,  9,  2,  4,  1,  9,  8,\n",
      "       ...\n",
      "        7,  9,  4,  4,  8,  1,  7,  8,  4,  8],\n",
      "      dtype='int32', name='time', length=17710)\n"
     ]
    }
   ],
   "source": [
    "# 当 time_value 是一个 DatetimeIndex 对象时，它代表了一个由 pandas 库创建的日期时间索引。\n",
    "# DatetimeIndex 对象包含了一系列的 datetime 对象，每个对象都具有年、月、日、时、分、秒等属性。\n",
    "#\n",
    "# 对于 time_value.day 和 time_value.year：\n",
    "#\n",
    "# time_value.day：这将返回一个长度与 DatetimeIndex 中元素数量相同的序列，其中每个元素对应于 DatetimeIndex 中相应 datetime 对象的日部分。time_value.day 是一个包含这些点的日（1到31的整数）的数组。\n",
    "#\n",
    "# time_value.year：与 .day 类似，.year 将返回一个序列，其中每个元素对应于 DatetimeIndex 中相应 datetime 对象的年部分。这将是一个包含这些点的年份（以四位数表示的整数）的数组\n",
    "print(time_value.year)\n",
    "print(type(time_value.year))\n",
    "print()\n",
    "\n",
    "print(time_value.month)\n",
    "print(time_value.day)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.142263500Z",
     "start_time": "2024-05-26T11:50:10.924853100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "(17710, 6)"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.143261700Z",
     "start_time": "2024-05-26T11:50:10.954774300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "      row_id       x       y  accuracy    time    place_id  day  hour  weekday\n600      600  1.2214  2.7023        17   65380  6683426742    1    18        3\n957      957  1.1832  2.6891        58  785470  6683426742   10     2        5\n4345    4345  1.1935  2.6550        11  400082  6889790653    5    15        0\n4735    4735  1.1452  2.6074        49  514983  6822359752    6    23        1\n5580    5580  1.0089  2.7287        19  732410  1527921905    9    11        4",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>row_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>time</th>\n      <th>place_id</th>\n      <th>day</th>\n      <th>hour</th>\n      <th>weekday</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>600</th>\n      <td>600</td>\n      <td>1.2214</td>\n      <td>2.7023</td>\n      <td>17</td>\n      <td>65380</td>\n      <td>6683426742</td>\n      <td>1</td>\n      <td>18</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>957</th>\n      <td>957</td>\n      <td>1.1832</td>\n      <td>2.6891</td>\n      <td>58</td>\n      <td>785470</td>\n      <td>6683426742</td>\n      <td>10</td>\n      <td>2</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>4345</th>\n      <td>4345</td>\n      <td>1.1935</td>\n      <td>2.6550</td>\n      <td>11</td>\n      <td>400082</td>\n      <td>6889790653</td>\n      <td>5</td>\n      <td>15</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4735</th>\n      <td>4735</td>\n      <td>1.1452</td>\n      <td>2.6074</td>\n      <td>49</td>\n      <td>514983</td>\n      <td>6822359752</td>\n      <td>6</td>\n      <td>23</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>5580</th>\n      <td>5580</td>\n      <td>1.0089</td>\n      <td>2.7287</td>\n      <td>19</td>\n      <td>732410</td>\n      <td>1527921905</td>\n      <td>9</td>\n      <td>11</td>\n      <td>4</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 构造一些特征，执行的警告是因为我们的操作是复制，loc是直接放入\n",
    "\n",
    "# data['day'] = time_value.day\n",
    "# data['hour'] = time_value.hour\n",
    "# data['weekday'] = time_value.weekday\n",
    "\n",
    "# 日期，是否是周末，小时对于个人行为的影响是较大的(例如吃饭时间去饭店，看电影时间去电影院等),所以才做下面的处理\n",
    "\n",
    "# insert 方法是Pandas提供的一个更为通用的方法，用于在DataFrame中的指定位置插入一列\n",
    "# data.shape[1]表示列数，所以就是插入到最后\n",
    "data.insert(data.shape[1], 'day', time_value.day) # 一个月的哪一天\n",
    "data.insert(data.shape[1], 'hour', time_value.hour)\n",
    "data.insert(data.shape[1], 'weekday', time_value.weekday) #0代表周一，6代表周日，星期几\n",
    "\n",
    "data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.351474200Z",
     "start_time": "2024-05-26T11:50:10.970730400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "      row_id       x       y  accuracy    place_id  day  hour  weekday\n600      600  1.2214  2.7023        17  6683426742    1    18        3\n957      957  1.1832  2.6891        58  6683426742   10     2        5\n4345    4345  1.1935  2.6550        11  6889790653    5    15        0\n4735    4735  1.1452  2.6074        49  6822359752    6    23        1\n5580    5580  1.0089  2.7287        19  1527921905    9    11        4",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>row_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>place_id</th>\n      <th>day</th>\n      <th>hour</th>\n      <th>weekday</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>600</th>\n      <td>600</td>\n      <td>1.2214</td>\n      <td>2.7023</td>\n      <td>17</td>\n      <td>6683426742</td>\n      <td>1</td>\n      <td>18</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>957</th>\n      <td>957</td>\n      <td>1.1832</td>\n      <td>2.6891</td>\n      <td>58</td>\n      <td>6683426742</td>\n      <td>10</td>\n      <td>2</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>4345</th>\n      <td>4345</td>\n      <td>1.1935</td>\n      <td>2.6550</td>\n      <td>11</td>\n      <td>6889790653</td>\n      <td>5</td>\n      <td>15</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4735</th>\n      <td>4735</td>\n      <td>1.1452</td>\n      <td>2.6074</td>\n      <td>49</td>\n      <td>6822359752</td>\n      <td>6</td>\n      <td>23</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>5580</th>\n      <td>5580</td>\n      <td>1.0089</td>\n      <td>2.7287</td>\n      <td>19</td>\n      <td>1527921905</td>\n      <td>9</td>\n      <td>11</td>\n      <td>4</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把时间戳特征删除\n",
    "data = data.drop(['time'], axis=1)\n",
    "data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.351474200Z",
     "start_time": "2024-05-26T11:50:11.016608900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "row_id        int64\nx           float64\ny           float64\naccuracy      int64\nplace_id      int64\nday           int32\nhour          int32\nweekday       int32\ndtype: object"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.dtypes"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.376997900Z",
     "start_time": "2024-05-26T11:50:11.042538800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "3"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 星期天的weekday值是6\n",
    "per = pd.Period('2024-05-09 00:00', 'h')\n",
    "per.weekday"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.378697400Z",
     "start_time": "2024-05-26T11:50:11.056274100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "             row_id             x             y      accuracy      place_id  \\\ncount  1.771000e+04  17710.000000  17710.000000  17710.000000  1.771000e+04   \nmean   1.450569e+07      1.122538      2.632309     82.482101  5.129895e+09   \nstd    8.353805e+06      0.077086      0.070144    113.613227  2.357399e+09   \nmin    6.000000e+02      1.000100      2.500100      1.000000  1.012024e+09   \n25%    7.327816e+06      1.049200      2.573800     25.000000  3.312464e+09   \n50%    1.443071e+07      1.123300      2.642300     62.000000  5.261906e+09   \n75%    2.163463e+07      1.190500      2.687800     75.000000  6.766325e+09   \nmax    2.911215e+07      1.249900      2.749900   1004.000000  9.980711e+09   \n\n                day          hour       weekday  \ncount  17710.000000  17710.000000  17710.000000  \nmean       5.101863     11.485545      3.092377  \nstd        2.709287      6.932195      1.680218  \nmin        1.000000      0.000000      0.000000  \n25%        3.000000      6.000000      2.000000  \n50%        5.000000     12.000000      3.000000  \n75%        7.000000     17.000000      4.000000  \nmax       10.000000     23.000000      6.000000  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>row_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>place_id</th>\n      <th>day</th>\n      <th>hour</th>\n      <th>weekday</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>count</th>\n      <td>1.771000e+04</td>\n      <td>17710.000000</td>\n      <td>17710.000000</td>\n      <td>17710.000000</td>\n      <td>1.771000e+04</td>\n      <td>17710.000000</td>\n      <td>17710.000000</td>\n      <td>17710.000000</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>1.450569e+07</td>\n      <td>1.122538</td>\n      <td>2.632309</td>\n      <td>82.482101</td>\n      <td>5.129895e+09</td>\n      <td>5.101863</td>\n      <td>11.485545</td>\n      <td>3.092377</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>8.353805e+06</td>\n      <td>0.077086</td>\n      <td>0.070144</td>\n      <td>113.613227</td>\n      <td>2.357399e+09</td>\n      <td>2.709287</td>\n      <td>6.932195</td>\n      <td>1.680218</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>6.000000e+02</td>\n      <td>1.000100</td>\n      <td>2.500100</td>\n      <td>1.000000</td>\n      <td>1.012024e+09</td>\n      <td>1.000000</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>7.327816e+06</td>\n      <td>1.049200</td>\n      <td>2.573800</td>\n      <td>25.000000</td>\n      <td>3.312464e+09</td>\n      <td>3.000000</td>\n      <td>6.000000</td>\n      <td>2.000000</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>1.443071e+07</td>\n      <td>1.123300</td>\n      <td>2.642300</td>\n      <td>62.000000</td>\n      <td>5.261906e+09</td>\n      <td>5.000000</td>\n      <td>12.000000</td>\n      <td>3.000000</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>2.163463e+07</td>\n      <td>1.190500</td>\n      <td>2.687800</td>\n      <td>75.000000</td>\n      <td>6.766325e+09</td>\n      <td>7.000000</td>\n      <td>17.000000</td>\n      <td>4.000000</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>2.911215e+07</td>\n      <td>1.249900</td>\n      <td>2.749900</td>\n      <td>1004.000000</td>\n      <td>9.980711e+09</td>\n      <td>10.000000</td>\n      <td>23.000000</td>\n      <td>6.000000</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 观察数据，看下是否有空值，异常值\n",
    "data.describe()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.421655600Z",
     "start_time": "2024-05-26T11:50:11.073448100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "            row_id     x     y  accuracy   day  hour  weekday\nplace_id                                                     \n1012023972       1     1     1         1     1     1        1\n1057182134       1     1     1         1     1     1        1\n1059958036       3     3     3         3     3     3        3\n1085266789       1     1     1         1     1     1        1\n1097200869    1044  1044  1044      1044  1044  1044     1044\n...            ...   ...   ...       ...   ...   ...      ...\n9904182060       1     1     1         1     1     1        1\n9915093501       1     1     1         1     1     1        1\n9946198589       1     1     1         1     1     1        1\n9950190890       1     1     1         1     1     1        1\n9980711012       5     5     5         5     5     5        5\n\n[805 rows x 7 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>row_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>day</th>\n      <th>hour</th>\n      <th>weekday</th>\n    </tr>\n    <tr>\n      <th>place_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1012023972</th>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1057182134</th>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1059958036</th>\n      <td>3</td>\n      <td>3</td>\n      <td>3</td>\n      <td>3</td>\n      <td>3</td>\n      <td>3</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>1085266789</th>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1097200869</th>\n      <td>1044</td>\n      <td>1044</td>\n      <td>1044</td>\n      <td>1044</td>\n      <td>1044</td>\n      <td>1044</td>\n      <td>1044</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>9904182060</th>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>9915093501</th>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>9946198589</th>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>9950190890</th>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>9980711012</th>\n      <td>5</td>\n      <td>5</td>\n      <td>5</td>\n      <td>5</td>\n      <td>5</td>\n      <td>5</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n<p>805 rows × 7 columns</p>\n</div>"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把签到数量少于n个目标位置删除，place_id是标签，即目标值\n",
    "place_count = data.groupby('place_id').count()\n",
    "place_count"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.466674500Z",
     "start_time": "2024-05-26T11:50:11.134285100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "count     805.000000\nmean       22.000000\nstd        88.955632\nmin         1.000000\n25%         1.000000\n50%         2.000000\n75%         5.000000\nmax      1044.000000\nName: x, dtype: float64"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "place_count['x'].describe()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.467672600Z",
     "start_time": "2024-05-26T11:50:11.166031900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "       place_id  row_id     x     y  accuracy   day  hour  weekday\n0    1097200869    1044  1044  1044      1044  1044  1044     1044\n1    1228935308     120   120   120       120   120   120      120\n2    1267801529      58    58    58        58    58    58       58\n3    1278040507      15    15    15        15    15    15       15\n4    1285051622      21    21    21        21    21    21       21\n..          ...     ...   ...   ...       ...   ...   ...      ...\n234  9741307878       5     5     5         5     5     5        5\n235  9753855529      21    21    21        21    21    21       21\n236  9806043737       6     6     6         6     6     6        6\n237  9809476069      23    23    23        23    23    23       23\n238  9980711012       5     5     5         5     5     5        5\n\n[239 rows x 8 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>place_id</th>\n      <th>row_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>day</th>\n      <th>hour</th>\n      <th>weekday</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1097200869</td>\n      <td>1044</td>\n      <td>1044</td>\n      <td>1044</td>\n      <td>1044</td>\n      <td>1044</td>\n      <td>1044</td>\n      <td>1044</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1228935308</td>\n      <td>120</td>\n      <td>120</td>\n      <td>120</td>\n      <td>120</td>\n      <td>120</td>\n      <td>120</td>\n      <td>120</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1267801529</td>\n      <td>58</td>\n      <td>58</td>\n      <td>58</td>\n      <td>58</td>\n      <td>58</td>\n      <td>58</td>\n      <td>58</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1278040507</td>\n      <td>15</td>\n      <td>15</td>\n      <td>15</td>\n      <td>15</td>\n      <td>15</td>\n      <td>15</td>\n      <td>15</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1285051622</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>234</th>\n      <td>9741307878</td>\n      <td>5</td>\n      <td>5</td>\n      <td>5</td>\n      <td>5</td>\n      <td>5</td>\n      <td>5</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>235</th>\n      <td>9753855529</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n    </tr>\n    <tr>\n      <th>236</th>\n      <td>9806043737</td>\n      <td>6</td>\n      <td>6</td>\n      <td>6</td>\n      <td>6</td>\n      <td>6</td>\n      <td>6</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>237</th>\n      <td>9809476069</td>\n      <td>23</td>\n      <td>23</td>\n      <td>23</td>\n      <td>23</td>\n      <td>23</td>\n      <td>23</td>\n      <td>23</td>\n    </tr>\n    <tr>\n      <th>238</th>\n      <td>9980711012</td>\n      <td>5</td>\n      <td>5</td>\n      <td>5</td>\n      <td>5</td>\n      <td>5</td>\n      <td>5</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n<p>239 rows × 8 columns</p>\n</div>"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把index变为0,1,2，3,4,5,6，从零开始排，原来的index是row_id\n",
    "# 只选择去的人大于3的数据，认为1,2,3的是噪音，这个地方去的人很少，不用推荐给其他人\n",
    "tf = place_count[place_count.row_id > 3].reset_index() # 去过人数大于3的的签到地点。这个row_id是随便选的\n",
    "tf  # 239个签到地点"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.468834400Z",
     "start_time": "2024-05-26T11:50:11.199432900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "(16918, 8)"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据设定的地点目标值，对原本的样本进行过滤\n",
    "# isin可以过滤某一列要在一组值\n",
    "# 其实去掉的样本并不多，因为去的人多的签到地点就那么几个\n",
    "data = data[ data['place_id'].isin(tf.place_id) ]\n",
    "data.shape"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.471661100Z",
     "start_time": "2024-05-26T11:50:11.224597800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "data": {
      "text/plain": "      row_id       x       y  accuracy    place_id  day  hour  weekday\n600      600  1.2214  2.7023        17  6683426742    1    18        3\n957      957  1.1832  2.6891        58  6683426742   10     2        5\n4345    4345  1.1935  2.6550        11  6889790653    5    15        0\n4735    4735  1.1452  2.6074        49  6822359752    6    23        1\n5580    5580  1.0089  2.7287        19  1527921905    9    11        4",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>row_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>place_id</th>\n      <th>day</th>\n      <th>hour</th>\n      <th>weekday</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>600</th>\n      <td>600</td>\n      <td>1.2214</td>\n      <td>2.7023</td>\n      <td>17</td>\n      <td>6683426742</td>\n      <td>1</td>\n      <td>18</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>957</th>\n      <td>957</td>\n      <td>1.1832</td>\n      <td>2.6891</td>\n      <td>58</td>\n      <td>6683426742</td>\n      <td>10</td>\n      <td>2</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>4345</th>\n      <td>4345</td>\n      <td>1.1935</td>\n      <td>2.6550</td>\n      <td>11</td>\n      <td>6889790653</td>\n      <td>5</td>\n      <td>15</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4735</th>\n      <td>4735</td>\n      <td>1.1452</td>\n      <td>2.6074</td>\n      <td>49</td>\n      <td>6822359752</td>\n      <td>6</td>\n      <td>23</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>5580</th>\n      <td>5580</td>\n      <td>1.0089</td>\n      <td>2.7287</td>\n      <td>19</td>\n      <td>1527921905</td>\n      <td>9</td>\n      <td>11</td>\n      <td>4</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.482631500Z",
     "start_time": "2024-05-26T11:50:11.255515600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(16918, 6)\n",
      "Index(['x', 'y', 'accuracy', 'day', 'hour', 'weekday'], dtype='object')\n"
     ]
    },
    {
     "data": {
      "text/plain": "           x       y  accuracy  day  hour  weekday\n600   1.2214  2.7023        17    1    18        3\n957   1.1832  2.6891        58   10     2        5\n4345  1.1935  2.6550        11    5    15        0\n4735  1.1452  2.6074        49    6    23        1\n5580  1.0089  2.7287        19    9    11        4",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>day</th>\n      <th>hour</th>\n      <th>weekday</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>600</th>\n      <td>1.2214</td>\n      <td>2.7023</td>\n      <td>17</td>\n      <td>1</td>\n      <td>18</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>957</th>\n      <td>1.1832</td>\n      <td>2.6891</td>\n      <td>58</td>\n      <td>10</td>\n      <td>2</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>4345</th>\n      <td>1.1935</td>\n      <td>2.6550</td>\n      <td>11</td>\n      <td>5</td>\n      <td>15</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4735</th>\n      <td>1.1452</td>\n      <td>2.6074</td>\n      <td>49</td>\n      <td>6</td>\n      <td>23</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>5580</th>\n      <td>1.0089</td>\n      <td>2.7287</td>\n      <td>19</td>\n      <td>9</td>\n      <td>11</td>\n      <td>4</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分离数据当中的特征值和目标值\n",
    "y = data['place_id']\n",
    "x = data.drop(['place_id'], axis=1)\n",
    "\n",
    "# 删除无用的特征值，row_id是索引,这就是噪音\n",
    "x = x.drop(['row_id'], axis=1)\n",
    "\n",
    "print(x.shape)\n",
    "print(x.columns)\n",
    "x.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.605012Z",
     "start_time": "2024-05-26T11:50:11.286764200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "source": [
    "print(type(x))\n",
    "print(type(y))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.606009600Z",
     "start_time": "2024-05-26T11:50:11.339222400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "x           float64\ny           float64\naccuracy      int64\nday           int32\nhour          int32\nweekday       int32\ndtype: object"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.dtypes"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.606009600Z",
     "start_time": "2024-05-26T11:50:11.339222400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [],
   "source": [
    "# 分割训练集、测试集\n",
    "# x_train，训练集的特征值\n",
    "# x_test，测试集的特征值\n",
    "# y_train，训练集的目标值\n",
    "# y_test，测试集的目标值，看不到\n",
    "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=1)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.645342200Z",
     "start_time": "2024-05-26T11:50:11.350387200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "<class 'pandas.core.series.Series'>\n",
      "<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "source": [
    "print(type(x_train))\n",
    "print(type(x_test))\n",
    "print(type(y_train))\n",
    "print(type(y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.647184800Z",
     "start_time": "2024-05-26T11:50:11.399547900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.12295735  2.63237278 81.34938525  5.10064628 11.44293821  3.10135561]\n",
      "[5.98489138e-03 4.86857391e-03 1.19597480e+04 7.32837915e+00\n",
      " 4.83742660e+01 2.81838404e+00]\n",
      "2158396011328\n",
      "--------------------------------------------------\n",
      "2158396024464\n",
      "[ 1.12295735  2.63237278 81.34938525  5.10064628 11.44293821  3.10135561]\n",
      "[5.98489138e-03 4.86857391e-03 1.19597480e+04 7.32837915e+00\n",
      " 4.83742660e+01 2.81838404e+00]\n"
     ]
    }
   ],
   "source": [
    "# 特征工程（标准化）,下面3行注释，一开始我们不进行标准化，看下效果，目标值要不要标准化？\n",
    "std = StandardScaler()\n",
    "\n",
    "# 对测试集和训练集的特征值进行标准化,服务于knn fit\n",
    "x_train = std.fit_transform(x_train)\n",
    "print(std.mean_)\n",
    "print(std.var_)\n",
    "print(id(x_test)) # transform返回的是copy，不在原有的输入对象中去修改\n",
    "\n",
    "print('-' * 50)\n",
    "\n",
    "x_test = std.transform(x_test) # transfrom不再进行均值和方差的计算，是在原有的基础上去标准化，因为假设train、test服从同一分布\n",
    "\n",
    "print(id(x_test))\n",
    "print(std.mean_)    # 对象std在创建时，就有了这两个属性。transform不会修改这两个值\n",
    "print(std.var_)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.658149700Z",
     "start_time": "2024-05-26T11:50:11.424714800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "<class 'numpy.ndarray'>\n",
      "<class 'pandas.core.series.Series'>\n",
      "<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "source": [
    "print(type(x_train))\n",
    "print(type(x_test))\n",
    "print(type(y_train))\n",
    "print(type(y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.659147500Z",
     "start_time": "2024-05-26T11:50:11.463687700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(12688, 6)\n",
      "(4230, 6)\n"
     ]
    }
   ],
   "source": [
    "print(x_train.shape)\n",
    "print(x_test.shape)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:11.660144300Z",
     "start_time": "2024-05-26T11:50:11.475651600Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 进行算法流程"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测的前10个签到位置为： [5689129232 1097200869 2355236719 9632980559 6424972551 4022692381\n",
      " 8048985799 6683426742 1435128522 3312463746]\n",
      "预测的准确率: 0.484160756501182\n"
     ]
    }
   ],
   "source": [
    "knn = KNeighborsClassifier(n_neighbors=6) # n_neighbors，参考的邻居数，默认为5\n",
    "\n",
    "# fit，knn的fit是不训练的，只是把训练集的特征值和目标值放入到内存中\n",
    "knn.fit(x_train, y_train)\n",
    "\n",
    "# predict，得出预测结果\n",
    "y_predict = knn.predict(x_test)\n",
    "print(\"预测的前10个签到位置为：\", y_predict[0:10])\n",
    "\n",
    "# score，得出准确率\n",
    "print(\"预测的准确率:\", knn.score(x_test, y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:12.408520900Z",
     "start_time": "2024-05-26T11:50:11.490100600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1970-01-10 02:23:38\n"
     ]
    }
   ],
   "source": [
    "print(max(time_value))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:12.440908Z",
     "start_time": "2024-05-26T11:50:12.133960600Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 网格搜索\n",
    "### 在k折交叉验证中，数据被分成k个不同的子集，每个子集作为测试集，其余的作为训练集。\n",
    "### 这个过程重复k次并计算模型的平均性能。然而，如果某个类别的样本数少于k，那么在至少一次的分割中，\n",
    "### 该类别在训练集或测试集中将没有样本，这会导致无法评估模型在该类别上的性能。"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\3.8\\lib\\site-packages\\sklearn\\model_selection\\_split.py:737: UserWarning: The least populated class in y has only 1 members, which is less than n_splits=3.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在测试集上准确率： 0.49763593380614657\n",
      "--------------------------------------------------\n",
      "在交叉验证中最好参数的结果： 0.4816362349278435\n",
      "--------------------------------------------------\n",
      "选择最好的模型是： KNeighborsClassifier(n_neighbors=12, weights='distance')\n",
      "--------------------------------------------------\n",
      "每个超参数每次交叉验证的结果：\n"
     ]
    },
    {
     "data": {
      "text/plain": "{'mean_fit_time': array([0.01408958, 0.0173409 , 0.02174473, 0.01888204, 0.02376731,\n        0.01772134, 0.01900498, 0.01626515, 0.01487064, 0.01597206]),\n 'std_fit_time': array([0.00062192, 0.00118757, 0.00459606, 0.00383295, 0.00344878,\n        0.00196093, 0.00221811, 0.00055889, 0.00116675, 0.00157878]),\n 'mean_score_time': array([0.16291968, 0.11190192, 0.25205127, 0.17893092, 0.40920297,\n        0.14401484, 0.22231102, 0.16050593, 0.23398987, 0.15721488]),\n 'std_score_time': array([0.00869252, 0.0141684 , 0.0246754 , 0.06577077, 0.15912323,\n        0.0062521 , 0.01286196, 0.00456237, 0.01153961, 0.01069997]),\n 'param_n_neighbors': masked_array(data=[3, 3, 5, 5, 10, 10, 12, 12, 15, 15],\n              mask=[False, False, False, False, False, False, False, False,\n                    False, False],\n        fill_value='?',\n             dtype=object),\n 'param_weights': masked_array(data=['uniform', 'distance', 'uniform', 'distance',\n                    'uniform', 'distance', 'uniform', 'distance',\n                    'uniform', 'distance'],\n              mask=[False, False, False, False, False, False, False, False,\n                    False, False],\n        fill_value='?',\n             dtype=object),\n 'params': [{'n_neighbors': 3, 'weights': 'uniform'},\n  {'n_neighbors': 3, 'weights': 'distance'},\n  {'n_neighbors': 5, 'weights': 'uniform'},\n  {'n_neighbors': 5, 'weights': 'distance'},\n  {'n_neighbors': 10, 'weights': 'uniform'},\n  {'n_neighbors': 10, 'weights': 'distance'},\n  {'n_neighbors': 12, 'weights': 'uniform'},\n  {'n_neighbors': 12, 'weights': 'distance'},\n  {'n_neighbors': 15, 'weights': 'uniform'},\n  {'n_neighbors': 15, 'weights': 'distance'}],\n 'split0_test_score': array([0.44468085, 0.4534279 , 0.4607565 , 0.47399527, 0.46170213,\n        0.48014184, 0.45650118, 0.48108747, 0.45508274, 0.47895981]),\n 'split1_test_score': array([0.43390873, 0.4542445 , 0.45660913, 0.47528967, 0.45542681,\n        0.48238354, 0.45329865, 0.48049184, 0.44809648, 0.47623552]),\n 'split2_test_score': array([0.43982029, 0.4561362 , 0.45684559, 0.47221565, 0.4618113 ,\n        0.48191062, 0.45897375, 0.48332939, 0.46062899, 0.48049184]),\n 'mean_test_score': array([0.43946996, 0.45460287, 0.45807041, 0.47383353, 0.45964675,\n        0.48147867, 0.45625786, 0.48163623, 0.45460274, 0.47856239]),\n 'std_test_score': array([0.00440467, 0.00113433, 0.00190181, 0.00126016, 0.00298428,\n        0.00096479, 0.00232323, 0.00122169, 0.00512762, 0.00176021]),\n 'rank_test_score': array([10,  8,  6,  4,  5,  2,  7,  1,  9,  3])}"
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "构造一些参数的值进行搜索\n",
    "\n",
    "weights参数是用于指定在进行决策时,根据距离来加权每个邻居对最终决策的贡献。\n",
    "\n",
    "weights可以取以下几种值：\n",
    "'uniform'：所有邻居的权重相同，不考虑它们与查询点的距离。在这种情况下，每个邻居对最终决策的贡献是相等的。\n",
    "'distance'：权重与邻居的距离成反比。这意味着距离查询点更近的邻居会有更大的权重。\n",
    "具体来说，每个邻居的权重是其距离的倒数。\n",
    "\n",
    "举个例子：\n",
    "如果最近的邻居是红色，它对分类决策的影响将大于稍远的邻居。\n",
    "即使另外两个邻居是蓝色，如果红色的邻居足够近，新点也可能被分类为红色。\n",
    "\"\"\"\n",
    "param = {\"n_neighbors\": [3, 5, 10, 12, 15],'weights':['uniform', 'distance']}\n",
    "\n",
    "# 进行网格搜索，cv=3是3折交叉验证，用其中2折训练，1折验证\n",
    "# 5*2=10组超参数，每组交叉3次，总共10*3=30次\n",
    "gc = GridSearchCV(knn, param_grid=param, cv=3)\n",
    "\n",
    "gc.fit(x_train, y_train)  #你给它的x_train，它又分为训练集，验证集\n",
    "\n",
    "print(\"在测试集上准确率：\", gc.score(x_test, y_test))\n",
    "print(\"-\"*50)\n",
    "\n",
    "print(\"在交叉验证中最好参数的结果：\", gc.best_score_)\n",
    "print(\"-\"*50)\n",
    "\n",
    "print(\"选择最好的模型是：\", gc.best_estimator_)\n",
    "print(\"-\"*50)\n",
    "\n",
    "print(\"每个超参数每次交叉验证的结果：\")\n",
    "gc.cv_results_"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:19.071942Z",
     "start_time": "2024-05-26T11:50:12.161016800Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 2、 朴素贝叶斯\n",
    "## 已知结果，推测导致该结果的原因，最大可能是哪个\n",
    "### 训练时间长，预测时间短\n",
    "### 用于文本分类\n",
    "### P(为科技类文章|该文本中出现了这些词) = P(该文本中出现了这些词 | 为科技类文章) * P(为科技类文章) / P(该文本中出现了这些词)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18846\n",
      "--------------------------------------------------\n",
      "From: Mamatha Devineni Ratnam <mr47+@andrew.cmu.edu>\n",
      "Subject: Pens fans reactions\n",
      "Organization: Post Office, Carnegie Mellon, Pittsburgh, PA\n",
      "Lines: 12\n",
      "NNTP-Posting-Host: po4.andrew.cmu.edu\n",
      "\n",
      "\n",
      "\n",
      "I am sure some bashers of Pens fans are pretty confused about the lack\n",
      "of any kind of posts about the recent Pens massacre of the Devils. Actually,\n",
      "I am  bit puzzled too and a bit relieved. However, I am going to put an end\n",
      "to non-PIttsburghers' relief with a bit of praise for the Pens. Man, they\n",
      "are killing those Devils worse than I thought. Jagr just showed you why\n",
      "he is much better than his regular season stats. He is also a lot\n",
      "fo fun to watch in the playoffs. Bowman should let JAgr have a lot of\n",
      "fun in the next couple of games since the Pens are going to beat the pulp out of Jersey anyway. I was very disappointed not to see the Islanders lose the final\n",
      "regular season game.          PENS RULE!!!\n",
      "\n",
      "\n",
      "--------------------------------------------------\n",
      "[10  3 17 ...  3  1  7]\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]\n",
      "['alt.atheism', 'comp.graphics', 'comp.os.ms-windows.misc', 'comp.sys.ibm.pc.hardware', 'comp.sys.mac.hardware', 'comp.windows.x', 'misc.forsale', 'rec.autos', 'rec.motorcycles', 'rec.sport.baseball', 'rec.sport.hockey', 'sci.crypt', 'sci.electronics', 'sci.med', 'sci.space', 'soc.religion.christian', 'talk.politics.guns', 'talk.politics.mideast', 'talk.politics.misc', 'talk.religion.misc']\n"
     ]
    }
   ],
   "source": [
    "news = fetch_20newsgroups(subset='all', data_home='data')\n",
    "\n",
    "print(len(news.data))  #样本数，因为是list，没有shape\n",
    "print('-'*50)\n",
    "\n",
    "print(news.data[0]) #第一个样本 特征\n",
    "print('-'*50)\n",
    "\n",
    "print(news.target) #标签\n",
    "print(np.unique(news.target)) #标签的类别，即有20类新闻\n",
    "print(news.target_names) #标签的名字"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:19.378879100Z",
     "start_time": "2024-05-26T11:50:19.051806600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [],
   "source": [
    "# 进行数据分割\n",
    "x_train, x_test, y_train, y_test = train_test_split(news.data, news.target, test_size=0.25, random_state=1)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:19.444120Z",
     "start_time": "2024-05-26T11:50:19.382119200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14134\n",
      "4712\n"
     ]
    }
   ],
   "source": [
    "print(len(x_train))\n",
    "print(len(x_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:19.456708800Z",
     "start_time": "2024-05-26T11:50:19.402958400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "<class 'list'>\n",
      "<class 'numpy.ndarray'>\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "print(type(x_train))\n",
    "print(type(x_test))\n",
    "print(type(y_train))\n",
    "print(type(y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:19.457707100Z",
     "start_time": "2024-05-26T11:50:19.417594300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "153196\n",
      "153196\n"
     ]
    }
   ],
   "source": [
    "# 对数据集进行特征抽取，因为是文本分类，所以用TF-IDF\n",
    "tf = TfidfVectorizer()\n",
    "\n",
    "# 以训练集当中的词的列表进行每篇文章重要性统计['a','b','c','d']\n",
    "x_train = tf.fit_transform(x_train)\n",
    "print(len(tf.get_feature_names_out())) # 得到特征数目，即分出来并最终保留了多少个单词\n",
    "\n",
    "x_test = tf.transform(x_test)\n",
    "print(len(tf.get_feature_names_out()))  # 特征数目不发生改变"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:24.287865100Z",
     "start_time": "2024-05-26T11:50:19.444120Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'scipy.sparse._csr.csr_matrix'>\n",
      "<class 'scipy.sparse._csr.csr_matrix'>\n",
      "<class 'numpy.ndarray'>\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "print(type(x_train))\n",
    "print(type(x_test))\n",
    "print(type(y_train))\n",
    "print(type(y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:24.297064800Z",
     "start_time": "2024-05-26T11:50:24.282090300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "murnane\n",
      "murph\n",
      "murphey\n",
      "conspiratorial\n"
     ]
    }
   ],
   "source": [
    "# 得到第十万个单词，我猜是按照数字+字母顺序排的\n",
    "print(tf.get_feature_names_out()[100002])\n",
    "print(tf.get_feature_names_out()[100003])\n",
    "print(tf.get_feature_names_out()[100004])\n",
    "print(tf.get_feature_names_out()[50000])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:25.144354700Z",
     "start_time": "2024-05-26T11:50:24.309033Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['00' '000' '0000' '00000' '0000000004' '0000000005' '0000000667'\n",
      " '0000001200' '000003' '000005102000']\n"
     ]
    }
   ],
   "source": [
    "print(tf.get_feature_names_out()[0:10])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:25.322168500Z",
     "start_time": "2024-05-26T11:50:25.299256700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "data": {
      "text/plain": "0.16678833961486816"
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "# 进行朴素贝叶斯算法的预测\n",
    "# alpha是拉普拉斯平滑系数，分子加上系数α，分母加上 α * 标签的种数（比如这里是20类新闻）\n",
    "# 避免样本中，某一个词在一类文章中出现0次，预测时如果某一篇该类文章出现了这个词，则分子乘积为0，不可能预测正确了\n",
    "mlt = MultinomialNB(alpha=1.0)\n",
    "\n",
    "# 训练，并统计训练时间\n",
    "start=time.time()\n",
    "mlt.fit(x_train, y_train)\n",
    "end=time.time()\n",
    "end-start"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:25.509456200Z",
     "start_time": "2024-05-26T11:50:25.322168500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测的前10篇文章类别为： [16 19 18  1  9 15  1  2 16 13]\n",
      "准确率为： 0.8518675721561969\n"
     ]
    },
    {
     "data": {
      "text/plain": "0.0742335319519043"
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "start=time.time()\n",
    "y_predict = mlt.predict(x_test)\n",
    "\n",
    "print(\"预测的前10篇文章类别为：\", y_predict[:10])\n",
    "\n",
    "# 得出准确率,这个是很难提高准确率，为什么呢？\n",
    "print(\"准确率为：\", mlt.score(x_test, y_test))\n",
    "end=time.time()\n",
    "end-start"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:25.605128800Z",
     "start_time": "2024-05-26T11:50:25.497489800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "outputs": [
    {
     "data": {
      "text/plain": "4712"
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 预测的文章数目\n",
    "len(y_predict)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:25.607123800Z",
     "start_time": "2024-05-26T11:50:25.588176600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "每个类别的精确率和召回率：\n",
      "                           precision    recall  f1-score   support\n",
      "\n",
      "             alt.atheism       0.91      0.77      0.83       199\n",
      "           comp.graphics       0.83      0.79      0.81       242\n",
      " comp.os.ms-windows.misc       0.89      0.83      0.86       263\n",
      "comp.sys.ibm.pc.hardware       0.80      0.83      0.81       262\n",
      "   comp.sys.mac.hardware       0.90      0.88      0.89       234\n",
      "          comp.windows.x       0.92      0.85      0.88       230\n",
      "            misc.forsale       0.96      0.67      0.79       257\n",
      "               rec.autos       0.90      0.87      0.88       265\n",
      "         rec.motorcycles       0.90      0.95      0.92       251\n",
      "      rec.sport.baseball       0.89      0.96      0.93       226\n",
      "        rec.sport.hockey       0.95      0.98      0.96       262\n",
      "               sci.crypt       0.76      0.97      0.85       257\n",
      "         sci.electronics       0.84      0.80      0.82       229\n",
      "                 sci.med       0.97      0.86      0.91       249\n",
      "               sci.space       0.92      0.96      0.94       256\n",
      "  soc.religion.christian       0.55      0.98      0.70       243\n",
      "      talk.politics.guns       0.76      0.96      0.85       234\n",
      "   talk.politics.mideast       0.93      0.99      0.96       224\n",
      "      talk.politics.misc       0.98      0.56      0.72       197\n",
      "      talk.religion.misc       0.97      0.26      0.41       132\n",
      "\n",
      "                accuracy                           0.85      4712\n",
      "               macro avg       0.88      0.84      0.84      4712\n",
      "            weighted avg       0.87      0.85      0.85      4712\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 目前这个场景我们不需要召回率，support是测试集中真实的为那个类别的有多少个样本\n",
    "print(\"每个类别的精确率和召回率：\\n\",\n",
    "      classification_report(y_test, y_predict,target_names=news.target_names))\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:25.719381900Z",
     "start_time": "2024-05-26T11:50:25.601233100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "230\n",
      "214\n",
      "AUC指标： 0.924078924393225\n"
     ]
    }
   ],
   "source": [
    "# 把0-19总计20个分类，取第5类算一算\n",
    "# 是第5类文章的变为1，不是的变为0\n",
    "y_test1 = np.where(y_test == 5, 1, 0)\n",
    "print(y_test1.sum())\n",
    "\n",
    "y_predict1 = np.where(y_predict == 5, 1, 0)\n",
    "print(y_predict1.sum())\n",
    "\n",
    "# roc_auc_score的y_test只能是二分类,针对多分类如何计算AUC\n",
    "print(\"AUC指标：\", roc_auc_score(y_test1, y_predict1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:25.720416200Z",
     "start_time": "2024-05-26T11:50:25.636928100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 算多分类的精确率，召回率，F1-score\n",
    "### 要把四象限给搞出来"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FP: 18\n",
      "TP: 196\n",
      "FN: 34\n",
      "TN: 4464\n"
     ]
    }
   ],
   "source": [
    "# 预测为1，实际为0，即没病判为有病\n",
    "FP = np.where((np.array(y_predict1)-np.array(y_test1))==1,1,0).sum()\n",
    "print(\"FP:\",FP)\n",
    "\n",
    "# 准确判断出有病\n",
    "TP = y_predict1.sum() - FP\n",
    "print(\"TP:\",TP)\n",
    "\n",
    "# 预测为0，实际为1，即有病判为没病\n",
    "FN = np.where((np.array(y_predict1)-np.array(y_test1))==-1,1,0).sum()\n",
    "print(\"FN:\",FN)\n",
    "\n",
    "# 准确判断出没病\n",
    "TN = len(y_predict1) - TP - FN - FP\n",
    "#TN = np.where(np.array(y_predict1)==0,1,0).sum() - FN\n",
    "#TN = np.where(np.array(y_test1)==0,1,0).sum() - FP\n",
    "print(\"TN:\",TN)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:25.721477900Z",
     "start_time": "2024-05-26T11:50:25.651886100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率precision: 0.9158878504672897\n",
      "召回率recall: 0.8521739130434782\n",
      "F1-score: 0.8828828828828829\n"
     ]
    }
   ],
   "source": [
    "print(\"准确率precision:\" , TP/(TP+FP))\n",
    "print(\"召回率recall:\" , TP/(TP+FN))\n",
    "print(\"F1-score:\" , 2*TP/(2*TP+FP+FN))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:25.721477900Z",
     "start_time": "2024-05-26T11:50:25.665848500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "outputs": [],
   "source": [
    "del news\n",
    "del x_train\n",
    "del x_test\n",
    "del y_train\n",
    "del y_test\n",
    "del y_predict\n",
    "del y_predict1\n",
    "del y_test1\n",
    "del tf"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:25.728494600Z",
     "start_time": "2024-05-26T11:50:25.713397Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 3 决策树"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split, GridSearchCV\n",
    "from sklearn.feature_extraction import DictVectorizer\n",
    "from sklearn.tree import DecisionTreeClassifier, export_graphviz\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "import pandas as pd\n",
    "import numpy as np\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:25.814977700Z",
     "start_time": "2024-05-26T11:50:25.728494600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "outputs": [
    {
     "data": {
      "text/plain": "-5.0"
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.log2(1/32)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:25.839883100Z",
     "start_time": "2024-05-26T11:50:25.741752300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "outputs": [
    {
     "data": {
      "text/plain": "-1.0"
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 / 2 * np.log2(1 /2) + 1 / 2 * np.log2(1 /2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:25.839883100Z",
     "start_time": "2024-05-26T11:50:25.754799500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "outputs": [
    {
     "data": {
      "text/plain": "-0.9182958340544896"
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 / 3 * np.log2(1 / 3) + 2 / 3 * np.log2(2 / 3)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:25.840881600Z",
     "start_time": "2024-05-26T11:50:25.771752300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1313 entries, 0 to 1312\n",
      "Data columns (total 11 columns):\n",
      " #   Column     Non-Null Count  Dtype  \n",
      "---  ------     --------------  -----  \n",
      " 0   row.names  1313 non-null   int64  \n",
      " 1   pclass     1313 non-null   object \n",
      " 2   survived   1313 non-null   int64  \n",
      " 3   name       1313 non-null   object \n",
      " 4   age        633 non-null    float64\n",
      " 5   embarked   821 non-null    object \n",
      " 6   home.dest  754 non-null    object \n",
      " 7   room       77 non-null     object \n",
      " 8   ticket     69 non-null     object \n",
      " 9   boat       347 non-null    object \n",
      " 10  sex        1313 non-null   object \n",
      "dtypes: float64(1), int64(2), object(8)\n",
      "memory usage: 113.0+ KB\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "决策树对泰坦尼克号进行预测生死\n",
    ":return: None\n",
    "\"\"\"\n",
    "# 获取数据\n",
    "titan = pd.read_csv(\"./data/titanic.txt\")\n",
    "titan.info()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:25.842104300Z",
     "start_time": "2024-05-26T11:50:25.788708600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1313 entries, 0 to 1312\n",
      "Data columns (total 3 columns):\n",
      " #   Column  Non-Null Count  Dtype  \n",
      "---  ------  --------------  -----  \n",
      " 0   pclass  1313 non-null   object \n",
      " 1   age     633 non-null    float64\n",
      " 2   sex     1313 non-null   object \n",
      "dtypes: float64(1), object(2)\n",
      "memory usage: 30.9+ KB\n",
      "None\n"
     ]
    },
    {
     "data": {
      "text/plain": "       pclass         age   sex\ncount    1313  633.000000  1313\nunique      3         NaN     2\ntop       3rd         NaN  male\nfreq      711         NaN   850\nmean      NaN   31.194181   NaN\nstd       NaN   14.747525   NaN\nmin       NaN    0.166700   NaN\n25%       NaN   21.000000   NaN\n50%       NaN   30.000000   NaN\n75%       NaN   41.000000   NaN\nmax       NaN   71.000000   NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>pclass</th>\n      <th>age</th>\n      <th>sex</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>count</th>\n      <td>1313</td>\n      <td>633.000000</td>\n      <td>1313</td>\n    </tr>\n    <tr>\n      <th>unique</th>\n      <td>3</td>\n      <td>NaN</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>top</th>\n      <td>3rd</td>\n      <td>NaN</td>\n      <td>male</td>\n    </tr>\n    <tr>\n      <th>freq</th>\n      <td>711</td>\n      <td>NaN</td>\n      <td>850</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>NaN</td>\n      <td>31.194181</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>NaN</td>\n      <td>14.747525</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>NaN</td>\n      <td>0.166700</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>NaN</td>\n      <td>21.000000</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>NaN</td>\n      <td>30.000000</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>NaN</td>\n      <td>41.000000</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>NaN</td>\n      <td>71.000000</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 特征选用 客舱等级、年龄、性别\n",
    "x = titan[['pclass', 'age', 'sex']]\n",
    "\n",
    "y = titan['survived']\n",
    "print(x.info())  # 用来判断是否有空值\n",
    "x.describe(include='all')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:26.032001300Z",
     "start_time": "2024-05-26T11:50:25.828913300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "outputs": [],
   "source": [
    "# 用均值填充缺失值\n",
    "mean = x['age'].mean()\n",
    "#x['age'] = x['age'].fillna(mean)  #对切片赋值，不会改变原来的df\n",
    "x.loc[:,'age'] = x.loc[:,'age'].fillna(mean)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:26.047177500Z",
     "start_time": "2024-05-26T11:50:25.855380600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1313 entries, 0 to 1312\n",
      "Data columns (total 3 columns):\n",
      " #   Column  Non-Null Count  Dtype  \n",
      "---  ------  --------------  -----  \n",
      " 0   pclass  1313 non-null   object \n",
      " 1   age     1313 non-null   float64\n",
      " 2   sex     1313 non-null   object \n",
      "dtypes: float64(1), object(2)\n",
      "memory usage: 30.9+ KB\n"
     ]
    }
   ],
   "source": [
    "x.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:26.080381100Z",
     "start_time": "2024-05-26T11:50:25.868063300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.series.Series'>\n",
      "RangeIndex: 1313 entries, 0 to 1312\n",
      "Series name: survived\n",
      "Non-Null Count  Dtype\n",
      "--------------  -----\n",
      "1313 non-null   int64\n",
      "dtypes: int64(1)\n",
      "memory usage: 10.4 KB\n"
     ]
    }
   ],
   "source": [
    "y.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:26.081094900Z",
     "start_time": "2024-05-26T11:50:25.887299400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "outputs": [],
   "source": [
    "# 分割数据集到训练集合测试集\n",
    "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=4)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:26.093571400Z",
     "start_time": "2024-05-26T11:50:25.903336700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "<class 'pandas.core.series.Series'>\n",
      "<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "source": [
    "print(type(x_train))\n",
    "print(type(x_test))\n",
    "print(type(y_train))\n",
    "print(type(y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:26.100041500Z",
     "start_time": "2024-05-26T11:50:25.916062700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    pclass        age     sex\n",
      "598    2nd  30.000000    male\n",
      "246    1st  62.000000    male\n",
      "905    3rd  31.194181  female\n",
      "300    1st  31.194181  female\n",
      "509    2nd  64.000000    male\n"
     ]
    }
   ],
   "source": [
    "print(x_train.head())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:26.100041500Z",
     "start_time": "2024-05-26T11:50:25.928079Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pclass    643\n",
      "age       643\n",
      "sex       643\n",
      "dtype: int64\n",
      "\n",
      "650\n",
      "334\n"
     ]
    }
   ],
   "source": [
    "# 男性的数量\n",
    "print(x_train[x_train['sex'] == 'male'].count())\n",
    "print()\n",
    "\n",
    "# 查看未存活、存活的人数\n",
    "print(y_train[y_train == 0].count())\n",
    "print(y_train.sum())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:26.101041100Z",
     "start_time": "2024-05-26T11:50:25.944039900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "outputs": [
    {
     "data": {
      "text/plain": "survived  sex   \n0         male      539\n          female    111\n1         female    230\n          male      104\nName: count, dtype: int64"
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看性别是否影响存活\n",
    "x_train.groupby(y_train)['sex'].value_counts()  # 按照y_train的0、1值来分组"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:26.123282400Z",
     "start_time": "2024-05-26T11:50:25.958121100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 因为数据中又有浮点数，又有字符串，所以要变成字典"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "outputs": [
    {
     "data": {
      "text/plain": "[{'pclass': '2nd', 'age': 30.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 62.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 64.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 24.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 21.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 23.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 44.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 37.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 6.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 41.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 30.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 25.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 24.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 57.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 25.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 36.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 51.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 43.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 16.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 19.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 13.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 45.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 36.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 22.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 30.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 16.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 47.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 20.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 57.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 58.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 30.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 61.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 38.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 27.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 35.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 58.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 40.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 32.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 21.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 54.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 19.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 38.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 39.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 52.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 60.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 11.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 21.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 45.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 18.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 28.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 19.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 21.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 21.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 65.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 30.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 28.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 55.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 22.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 36.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 45.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 24.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 20.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 24.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 15.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 26.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 45.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 35.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 22.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 48.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 48.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 30.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 8.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 21.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 27.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 45.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 30.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 25.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 18.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 5.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 25.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 39.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 19.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 36.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 33.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 9.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 26.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 23.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 17.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 36.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 58.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 12.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 23.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 28.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 18.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 36.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 32.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 37.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 25.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 49.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 18.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 44.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 17.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 23.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 32.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 26.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 52.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 28.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 40.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 34.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 27.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 20.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 22.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 40.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 33.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 18.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 37.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 24.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 15.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 28.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 21.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 30.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 20.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 44.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 36.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 39.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 13.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 36.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 12.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 2.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 26.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 27.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 64.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 40.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 42.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 3.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 5.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 22.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 36.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 33.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 45.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 26.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 20.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 18.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 22.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 54.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 11.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 49.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 39.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 22.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 32.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 45.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 10.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 36.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 18.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 18.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 22.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 20.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 44.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 30.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 38.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 27.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 36.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 20.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 6.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 16.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 0.3333, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 25.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 18.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 21.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 17.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 55.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 30.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 21.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 23.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 40.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 48.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 33.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 49.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 47.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 36.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 48.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 45.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 43.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 52.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 28.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 39.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 9.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 34.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 23.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 49.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 27.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 59.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 28.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 41.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 18.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 27.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 33.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 39.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 8.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 43.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 44.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 30.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 31.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 36.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 25.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 26.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 30.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 26.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 32.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 18.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 61.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 29.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 29.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 20.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 52.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 42.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 2.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 18.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 25.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 17.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 15.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 19.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 49.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 45.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 20.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 3.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 28.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 37.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 57.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 24.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 30.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 54.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 3.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 24.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 28.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 24.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 25.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 17.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 45.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 48.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 23.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 30.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 49.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 0.1667, 'sex': 'female'},\n {'pclass': '2nd', 'age': 8.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 35.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 34.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 23.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 33.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 7.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 27.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 18.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 50.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 39.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 50.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 26.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 46.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 35.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 25.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 30.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 24.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 17.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 27.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 33.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 60.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 58.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 20.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 33.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 38.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 30.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 21.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 54.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 63.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 0.9167, 'sex': 'male'},\n {'pclass': '3rd', 'age': 11.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 21.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 19.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 24.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 43.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 2.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 25.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 45.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 26.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 56.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 19.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 19.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 4.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 45.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 22.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 64.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 32.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 21.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 17.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 55.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 34.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 43.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 17.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 51.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 35.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 22.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 24.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 21.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 26.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 26.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 27.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 53.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 40.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 41.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 36.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 69.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 24.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 30.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 28.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 29.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 24.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 49.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 22.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 26.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 48.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 47.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 24.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 32.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 53.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 45.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 67.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 2.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 32.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 2.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 28.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 35.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 28.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 6.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 50.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 36.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 26.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 56.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 19.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 48.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 1.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 27.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 16.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 21.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 21.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 1.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 30.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 16.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 25.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 4.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 30.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 23.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 16.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 36.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 23.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 40.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 58.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 60.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 32.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 39.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 18.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 16.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 18.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 25.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 27.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 42.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 38.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 32.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 20.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 13.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 32.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 45.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 18.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 29.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 20.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 40.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 32.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 32.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 23.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 3.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 35.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 37.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 47.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 21.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 22.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 22.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 25.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 48.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 24.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 25.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 45.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 9.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 59.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 19.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 19.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 42.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 34.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 42.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 29.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 38.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 33.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 19.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 41.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 24.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 34.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 33.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 5.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 18.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 30.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 42.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 14.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 29.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 30.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 48.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 18.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 35.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 20.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 42.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 17.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 24.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 71.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 29.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 23.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 4.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 30.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 22.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 19.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 30.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 40.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 25.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 6.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 50.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '1st', 'age': 71.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 9.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 23.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 28.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 53.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 71.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 46.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 49.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 1.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 46.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 37.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 34.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 22.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 19.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '1st', 'age': 46.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 58.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 22.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 45.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 35.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 41.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 46.0, 'sex': 'female'},\n {'pclass': '1st', 'age': 36.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 24.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 44.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 57.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 9.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 23.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 24.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 47.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 50.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 50.0, 'sex': 'female'},\n {'pclass': '2nd', 'age': 34.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 38.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 22.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 18.0, 'sex': 'female'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n {'pclass': '3rd', 'age': 28.0, 'sex': 'male'},\n {'pclass': '2nd', 'age': 34.0, 'sex': 'male'},\n {'pclass': '1st', 'age': 46.0, 'sex': 'male'},\n {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'}]"
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把df变为字典（列表嵌套字典），records代表列名变为键\n",
    "x_train.to_dict(orient=\"records\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:26.234626400Z",
     "start_time": "2024-05-26T11:50:25.990053400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "outputs": [
    {
     "data": {
      "text/plain": "     pclass        age     sex\n598     2nd  30.000000    male\n246     1st  62.000000    male\n905     3rd  31.194181  female\n300     1st  31.194181  female\n509     2nd  64.000000    male\n...     ...        ...     ...\n360     2nd  31.194181    male\n709     3rd  28.000000    male\n439     2nd  34.000000    male\n174     1st  46.000000    male\n1146    3rd  31.194181    male\n\n[984 rows x 3 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>pclass</th>\n      <th>age</th>\n      <th>sex</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>598</th>\n      <td>2nd</td>\n      <td>30.000000</td>\n      <td>male</td>\n    </tr>\n    <tr>\n      <th>246</th>\n      <td>1st</td>\n      <td>62.000000</td>\n      <td>male</td>\n    </tr>\n    <tr>\n      <th>905</th>\n      <td>3rd</td>\n      <td>31.194181</td>\n      <td>female</td>\n    </tr>\n    <tr>\n      <th>300</th>\n      <td>1st</td>\n      <td>31.194181</td>\n      <td>female</td>\n    </tr>\n    <tr>\n      <th>509</th>\n      <td>2nd</td>\n      <td>64.000000</td>\n      <td>male</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>360</th>\n      <td>2nd</td>\n      <td>31.194181</td>\n      <td>male</td>\n    </tr>\n    <tr>\n      <th>709</th>\n      <td>3rd</td>\n      <td>28.000000</td>\n      <td>male</td>\n    </tr>\n    <tr>\n      <th>439</th>\n      <td>2nd</td>\n      <td>34.000000</td>\n      <td>male</td>\n    </tr>\n    <tr>\n      <th>174</th>\n      <td>1st</td>\n      <td>46.000000</td>\n      <td>male</td>\n    </tr>\n    <tr>\n      <th>1146</th>\n      <td>3rd</td>\n      <td>31.194181</td>\n      <td>male</td>\n    </tr>\n  </tbody>\n</table>\n<p>984 rows × 3 columns</p>\n</div>"
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_train"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T11:50:26.235624500Z",
     "start_time": "2024-05-26T11:50:26.094475300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "['age' 'pclass=1st' 'pclass=2nd' 'pclass=3rd' 'sex=female' 'sex=male']\n",
      "--------------------------------------------------\n",
      "[[30.          0.          1.          0.          0.          1.        ]\n",
      " [62.          1.          0.          0.          0.          1.        ]\n",
      " [31.19418104  0.          0.          1.          1.          0.        ]\n",
      " ...\n",
      " [34.          0.          1.          0.          0.          1.        ]\n",
      " [46.          1.          0.          0.          0.          1.        ]\n",
      " [31.19418104  0.          0.          1.          0.          1.        ]]\n"
     ]
    }
   ],
   "source": [
    "# 进行处理（特征工程）特征-》类别-》one_hot编码\n",
    "dict = DictVectorizer(sparse=False)\n",
    "\n",
    "# 这一步是对字典进行特征抽取,to_dict可以把df变为字典，records代表列名变为键\n",
    "x_train = dict.fit_transform(x_train.to_dict(orient=\"records\"))\n",
    "\n",
    "print(type(x_train))\n",
    "print(dict.get_feature_names_out())\n",
    "print('-' * 50)\n",
    "\n",
    "x_test = dict.transform(x_test.to_dict(orient=\"records\"))\n",
    "print(x_train)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-22T12:53:12.452061300Z",
     "start_time": "2024-05-22T12:53:12.381857600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "<class 'numpy.ndarray'>\n",
      "<class 'pandas.core.series.Series'>\n",
      "<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "source": [
    "print(type(x_train))\n",
    "print(type(x_test))\n",
    "print(type(y_train))\n",
    "print(type(y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-22T12:53:14.939646700Z",
     "start_time": "2024-05-22T12:53:14.908086200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测的准确率： 0.8085106382978723\n"
     ]
    }
   ],
   "source": [
    "# 用决策树进行预测，这里使用默认超参\n",
    "dec = DecisionTreeClassifier()\n",
    "\n",
    "# 训练\n",
    "dec.fit(x_train, y_train)\n",
    "\n",
    "# 预测准确率\n",
    "print(\"预测的准确率：\", dec.score(x_test, y_test))\n",
    "\n",
    "# 导出决策树的结构\n",
    "export_graphviz(dec, out_file=\"tree.dot\",\n",
    "                feature_names=['年龄', 'pclass=1st', 'pclass=2nd', 'pclass=3rd', '女性', '男性'])\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-22T12:53:17.413406800Z",
     "start_time": "2024-05-22T12:53:17.345333500Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 调整决策树的参数\n",
    "### 预剪枝和后剪枝"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测的准确率： 0.8206686930091185\n"
     ]
    }
   ],
   "source": [
    "# 分割数据集到训练集合测试集\n",
    "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=4)\n",
    "\n",
    "# 进行处理（特征工程）特征-》类别-》one_hot编码\n",
    "dict = DictVectorizer(sparse=False)\n",
    "\n",
    "# 这一步是对字典进行特征抽取\n",
    "x_train = dict.fit_transform(x_train.to_dict(orient=\"records\"))\n",
    "x_test = dict.transform(x_test.to_dict(orient=\"records\"))\n",
    "\n",
    "# print(x_train)\n",
    "# 用决策树进行预测，修改max_depth最大深度为7，min_impurity_decrease带来的增益要大于0.01才会进行划分\n",
    "dec = DecisionTreeClassifier(max_depth=7,min_impurity_decrease=0.01)\n",
    "\n",
    "# 训练\n",
    "dec.fit(x_train, y_train)\n",
    "\n",
    "# 预测准确率\n",
    "print(\"预测的准确率：\", dec.score(x_test, y_test))\n",
    "\n",
    "# 导出决策树的结构\n",
    "export_graphviz(dec, out_file=\"tree1.dot\",\n",
    "                feature_names=dict.get_feature_names_out())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-12T08:45:33.692266700Z",
     "start_time": "2024-05-12T08:45:33.638719300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "<class 'numpy.ndarray'>\n",
      "<class 'pandas.core.series.Series'>\n",
      "<class 'pandas.core.series.Series'>\n",
      "<class 'sklearn.feature_extraction._dict_vectorizer.DictVectorizer'>\n"
     ]
    }
   ],
   "source": [
    "print(type(x_train))\n",
    "print(type(x_test))\n",
    "print(type(y_train))\n",
    "print(type(y_test))\n",
    "print(type(dict))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-12T08:51:00.817463700Z",
     "start_time": "2024-05-12T08:51:00.764015100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 4、  随机森林"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "在随机森林（Random Forest, RF）算法中，\"随机放回抽样\"（也称为\"自助采样\"或\"Bootstrap抽样\"）是一种用于构建决策树的采样技术。具体来说，随机放回抽样指的是以下过程：\n",
    "\n",
    "1. **样本抽样**：从原始训练数据集中随机选择样本，每次抽取一个样本后，将其放回原样本池中，然后再进行下一次抽取。这意味着同一个样本可能被多次选中，而某些样本可能一次也不被选中。\n",
    "\n",
    "2. **特征选择**：在决策树的每个分裂节点，随机森林不是考虑所有可用的特征，而是随机选择一部分特征来寻找最佳的分裂。这样做可以增加模型的多样性，从而提高整个森林的泛化能力。\n",
    "\n",
    "3. **构建决策树**：使用上述通过随机放回抽样得到的训练样本集，构建一棵决策树。由于样本和特征选择的随机性，每棵树都有其独特的结构和规则。\n",
    "\n",
    "4. **重复过程**：重复上述过程多次，每次使用不同的随机放回抽样集，构建多棵决策树。这些树构成了所谓的“森林”。\n",
    "\n",
    "5. **聚合结果**：对于分类问题，随机森林通过多数投票的方式来预测新样本的类别；对于回归问题，则通常取多棵树预测结果的平均值作为最终预测。\n",
    "\n",
    "随机放回抽样的两个主要优点是：\n",
    "\n",
    "- **减少过拟合**：由于每棵树都是基于不同的样本集训练的，这有助于减少模型对训练数据的过度拟合。\n",
    "\n",
    "- **提高模型泛化能力**：不同的树可能会捕捉到数据中的不同模式，当聚合这些树的预测结果时，可以提高模型对未见数据的预测能力。\n",
    "\n",
    "随机放回抽样是随机森林算法的核心特性之一，它与模型的多样性和最终性能紧密相关。"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率： 0.8328267477203647\n",
      "查看选择的参数模型： {'max_depth': 3, 'n_estimators': 1500}\n",
      "选择最好的模型是： RandomForestClassifier(max_depth=3, n_estimators=1500, n_jobs=-1)\n",
      "每个超参数每次交叉验证的结果： {'mean_fit_time': array([2.28849896, 2.76052721, 6.12217911, 1.84454727, 2.4910597 ,\n",
      "       6.0712924 , 1.89149014, 2.69044797, 6.7603368 , 2.11728303,\n",
      "       2.76811814, 7.25228413, 2.01534144, 2.59912594, 6.92891463,\n",
      "       2.13015564, 2.90013242, 7.20528579]), 'std_fit_time': array([0.35604363, 0.02874893, 0.05889368, 0.00968632, 0.02193942,\n",
      "       0.01616449, 0.01688164, 0.18266398, 0.21465847, 0.09436413,\n",
      "       0.02290147, 0.06624282, 0.09978849, 0.08227633, 0.36963147,\n",
      "       0.0527389 , 0.03325693, 0.01760952]), 'mean_score_time': array([0.27455767, 0.35532331, 0.80679178, 0.248691  , 0.33048058,\n",
      "       0.79566757, 0.25678555, 0.36929886, 0.87495987, 0.27776051,\n",
      "       0.34911609, 1.09960977, 0.26023714, 0.33845282, 0.88979514,\n",
      "       0.26578172, 0.36563802, 0.87962596]), 'std_score_time': array([0.01988236, 0.01539482, 0.00701178, 0.0053444 , 0.00385955,\n",
      "       0.00352636, 0.00302078, 0.04722441, 0.04576924, 0.00271884,\n",
      "       0.01784156, 0.28515994, 0.01337428, 0.00283527, 0.03587083,\n",
      "       0.01693275, 0.00828919, 0.04226581]), 'param_max_depth': masked_array(data=[2, 2, 2, 3, 3, 3, 5, 5, 5, 8, 8, 8, 15, 15, 15, 25, 25,\n",
      "                   25],\n",
      "             mask=[False, False, False, False, False, False, False, False,\n",
      "                   False, False, False, False, False, False, False, False,\n",
      "                   False, False],\n",
      "       fill_value='?',\n",
      "            dtype=object), 'param_n_estimators': masked_array(data=[1500, 2000, 5000, 1500, 2000, 5000, 1500, 2000, 5000,\n",
      "                   1500, 2000, 5000, 1500, 2000, 5000, 1500, 2000, 5000],\n",
      "             mask=[False, False, False, False, False, False, False, False,\n",
      "                   False, False, False, False, False, False, False, False,\n",
      "                   False, False],\n",
      "       fill_value='?',\n",
      "            dtype=object), 'params': [{'max_depth': 2, 'n_estimators': 1500}, {'max_depth': 2, 'n_estimators': 2000}, {'max_depth': 2, 'n_estimators': 5000}, {'max_depth': 3, 'n_estimators': 1500}, {'max_depth': 3, 'n_estimators': 2000}, {'max_depth': 3, 'n_estimators': 5000}, {'max_depth': 5, 'n_estimators': 1500}, {'max_depth': 5, 'n_estimators': 2000}, {'max_depth': 5, 'n_estimators': 5000}, {'max_depth': 8, 'n_estimators': 1500}, {'max_depth': 8, 'n_estimators': 2000}, {'max_depth': 8, 'n_estimators': 5000}, {'max_depth': 15, 'n_estimators': 1500}, {'max_depth': 15, 'n_estimators': 2000}, {'max_depth': 15, 'n_estimators': 5000}, {'max_depth': 25, 'n_estimators': 1500}, {'max_depth': 25, 'n_estimators': 2000}, {'max_depth': 25, 'n_estimators': 5000}], 'split0_test_score': array([0.73780488, 0.73780488, 0.73780488, 0.80182927, 0.80182927,\n",
      "       0.80182927, 0.81097561, 0.81097561, 0.81097561, 0.82012195,\n",
      "       0.82012195, 0.82012195, 0.81402439, 0.81402439, 0.82012195,\n",
      "       0.81402439, 0.82012195, 0.82012195]), 'split1_test_score': array([0.82621951, 0.82621951, 0.82621951, 0.82317073, 0.82317073,\n",
      "       0.82317073, 0.81402439, 0.81402439, 0.81402439, 0.80792683,\n",
      "       0.80792683, 0.80792683, 0.81707317, 0.81707317, 0.81402439,\n",
      "       0.81402439, 0.81097561, 0.81707317]), 'split2_test_score': array([0.81707317, 0.81707317, 0.81707317, 0.82926829, 0.82926829,\n",
      "       0.82926829, 0.82317073, 0.82317073, 0.82317073, 0.79268293,\n",
      "       0.79268293, 0.79268293, 0.79573171, 0.79573171, 0.79573171,\n",
      "       0.79573171, 0.79573171, 0.79573171]), 'mean_test_score': array([0.79369919, 0.79369919, 0.79369919, 0.81808943, 0.81808943,\n",
      "       0.81808943, 0.81605691, 0.81605691, 0.81605691, 0.80691057,\n",
      "       0.80691057, 0.80691057, 0.80894309, 0.80894309, 0.80995935,\n",
      "       0.80792683, 0.80894309, 0.81097561]), 'std_test_score': array([0.03969924, 0.03969924, 0.03969924, 0.01176406, 0.01176406,\n",
      "       0.01176406, 0.00518193, 0.00518193, 0.00518193, 0.01122496,\n",
      "       0.01122496, 0.01122496, 0.00942441, 0.00942441, 0.01036386,\n",
      "       0.00862325, 0.01006046, 0.01085069]), 'rank_test_score': array([16, 16, 16,  1,  1,  1,  4,  4,  4, 13, 13, 13,  9,  9,  8, 12,  9,\n",
      "        7])}\n"
     ]
    }
   ],
   "source": [
    "# 随机森林进行预测 （超参数调优），n_jobs充分利用多核的一个参数\n",
    "rf = RandomForestClassifier(n_jobs=-1)\n",
    "\n",
    "# n_estimators森林中指定了要构建的决策树的数量，也就是分类器的数目\n",
    "# 在随机森林中，增加 n_estimators 通常会提高模型的性能，但同时也会提高计算成本。\n",
    "# max_depth  指定了决策树的最大深度。限制树的深度可以防止过拟合，但太浅的树可能导致欠拟合\n",
    "param = {\"n_estimators\": [1500, 2000, 5000], \"max_depth\": [2, 3, 5, 8, 15, 25]}\n",
    "\n",
    "# 网格搜索与交叉验证\n",
    "gc = GridSearchCV(rf, param_grid=param, cv=3)\n",
    "\n",
    "gc.fit(x_train, y_train)\n",
    "\n",
    "print(\"准确率：\", gc.score(x_test, y_test))\n",
    "\n",
    "print(\"查看选择的参数模型：\", gc.best_params_)\n",
    "\n",
    "print(\"选择最好的模型是：\", gc.best_estimator_)\n",
    "print(\"-\"*50)\n",
    "\n",
    "print(\"每个超参数每次交叉验证的结果：\\n\", gc.cv_results_)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-12T09:06:38.733596600Z",
     "start_time": "2024-05-12T09:02:42.644219500Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
